Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and mcolumns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column.

Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j].

There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.

If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.

Input

The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).

Output

The output contains a single number — the maximum total gain possible.

Example

Input
3 3
100 100 100
100 1 100
100 100 100
Output
800

Note

Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].

题意:一个人从左上角走到右下角,另一个人从左下角走到右上角,格子上有数,求两个人得到的最大格子数之和为多少。两人只能在一个格子相遇,这个格子的值不计入最后结果。(当然,他们一个只能向下和向右走,另一个只能向上和向右走。)

我觉得这道题好棒!虽然不会。实在不知道这怎么dp。

我没有发现一个重要的性质:当两人相遇后,他们只能沿各自原来的方向走。

画个示意图:

  

按照规则俩人相遇后只能按照原来的方向走才不会有多个交点,这就推出他们在边界不可能相遇。所以每次O((n-1)^2)的复杂度枚举交点,按四个方向递推dp求解。至于为什么要四个方向dp而不是两个,可以对照上图或者手动画一画想想。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = ;
int dp1[MAXN][MAXN], dp2[MAXN][MAXN], dp3[MAXN][MAXN], dp4[MAXN][MAXN];
int a[MAXN][MAXN];
int ans; int max(int a, int b)
{
if (a < b) return b;
return a;
} int main()
{
int n, m;
cin >> n >> m;
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
cin >> a[i][j];
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
dp1[i][j] = a[i][j] + max(dp1[i - ][j], dp1[i][j - ]);
for (int i = n; i >= ; i--)
for (int j = m; j >= ; j--)
dp2[i][j] = a[i][j] + max(dp2[i + ][j], dp2[i][j + ]);
for (int i = n; i >= ; i--)
for (int j = ; j <= m; j++)
dp3[i][j] = a[i][j] + max(dp3[i][j - ], dp3[i + ][j]);
for (int i = ; i <= n; i++)
for (int j = m; j >= ; j--)
dp4[i][j] = a[i][j] + max(dp4[i - ][j], dp4[i][j + ]);
ans = ;
for (int i = ; i<n; i++)
for (int j = ; j < m; j++) {
ans = max(ans, dp1[i - ][j] + dp2[i + ][j] + dp3[i][j - ] + dp4[i][j + ]);
ans = max(ans, dp1[i][j - ] + dp2[i][j + ] + dp3[i + ][j] + dp4[i - ][j]);
}
cout << ans << endl;
return ;
}

参考博客(感谢~):

【1】:http://blog.csdn.net/qq_34374664/article/details/54577940

【2】:http://blog.csdn.net/tc_to_top/article/details/51875673

Code Force 429B Working out【递推dp】的更多相关文章

  1. 递推DP URAL 1167 Bicolored Horses

    题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...

  2. 递推DP URAL 1017 Staircases

    题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...

  3. 递推DP URAL 1260 Nudnik Photographer

    题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...

  4. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  5. 递推DP URAL 1119 Metro

    题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...

  6. 递推DP 赛码 1005 Game

    题目传送门 /* 递推DP:官方题解 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩 ...

  7. 递推DP HDOJ 5328 Problem Killer

    题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...

  8. hdu1978(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1978 分析: 递推DP. dp[][]表示可以到达改点的方法数. 刚开始:外循环扫描所有点dp[x][ ...

  9. 递推DP URAL 1031 Railway Tickets

    题目传送门 /* 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 注意:s1与s2大小不一定,坑! 详细解释:http://blog.csdn.net/kk303/article/d ...

随机推荐

  1. Django项目:CRM(客户关系管理系统)--42--34PerfectCRM实现CRM自定义用户

    #models.py # ————————01PerfectCRM基本配置ADMIN———————— from django.db import models # Create your models ...

  2. Mac 安装package control

    参考网址:https://packagecontrol.io/installation 复制命令就不说明了. 主要阐述下第二种,下载安装包覆盖的方法,下面不说话,依次截图.步骤可以查看参考网址 啰嗦一 ...

  3. 基于jquery实现图片上传本地预览功能

    一.原理 分为两步: 当上传图片的input被触发并选择本地图片之后获取要上传的图片这个对象的URL(对象URL),把对象URL赋值给事先写好的img标签的src属性即可把图片显示出来.在这里,我们需 ...

  4. 使用yarn代替npm

    npm node module package,是nodeJs的包管理工具,最初是有 Isaac Z. Schlueter 开发的,这个让全世界的人都可以很快的运用互相开发的package的工具使no ...

  5. 读书笔记--Head First 设计模式 目录

    1.设计模式入门 2.观察者模式 3.装饰者模式 4.工厂模式 5.单件模式 6.命令模式 7.适配器模式与外观模式 8.模板方法模式 9.迭代器与组合模式 10.状态模式 11.代理模式 12.复合 ...

  6. javascript之键盘事件的方法

    键盘事件包含onkeydown.onkeypress和onkeyup这三个事件 事件初始化 function keyDown(){} document.onkeydown = keyDown; //论 ...

  7. Laravel 某个字段更新失败的原因

    明明有这个title, 但是却始终更新不成功 原因是模型这里设置了可以更新的字段,所以直接用Db::table更新会成功

  8. ubuntu安装django

    sudo apt-get install python-django -y#django操作mysql数据库时还需要安装python-mysqldb驱动,当然mysql安装是必须的前提 sudo ap ...

  9. 发布Qt Widgets桌面应用程序的方法

    Qt是一款优秀的跨平台开发框架,它可以在桌面.移动平台以及嵌入式平台上运行.目前Qt 5介绍程序发布的文章帖子比较少.大家又非常想要知道如何发布Qt应用程序,于是我花了一点儿时间介绍一下如何发布Qt桌 ...

  10. Hdu 1150

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...