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. LUOGU P1290 欧几里德的游戏

    题目描述 欧几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数M和N,从Stan开始,从其中较大的一个数,减去较小的数的正整数倍,当然,得到的数 ...

  2. mac 将本地文件上传到vps

    打开mac终端 假设通过ssh连接远程vps命令为. ssh root@194.10.10.23 -p92322 (说明:92322表示端口号,一般vps端口号默认是22) 那么复制本地文件到终端的命 ...

  3. Elasticsearch入门学习重点笔记

    原文:Elasticsearch入门学习重点笔记 必记知识点 Elasticsearch可以接近实时的搜索和存储大量数据.Elasticsearch是一个近实时的搜索平台.这意味着当你导入一个文档并把 ...

  4. Angular js 具体应用(一)

    1,首先引用Angular  百度静态资源库搜索Angular  复制链接,在HTML中嵌入script 最好写在正文下面 <script type="text/javascript& ...

  5. Leetcode59. Spiral Matrix II螺旋矩阵2

    给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...

  6. css3烟花效果

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. 学习JDK1.8集合源码之--LinkedHashSet

    1. LinkedHashSet简介 LinkedHashSet继承自HashSet,故拥有HashSet的全部API,LinkedHashSet内部实现简单,核心参数和方法都继承自HashSet,只 ...

  8. HTML5小知识汇总

    1.关于<!DOCTYPE HTML> H5只需要<!DOCTYPE HTML>这样简单的声明,不用之前一长串代码,因为H5不是基于SGML,所以不需要对DTD引用,但是需要D ...

  9. JSP Web第八章整理复习 过滤器

    P269  Filter过滤器的基本原理 P269  Filter过滤器体系结构 原理和体系结构看懂了就行 P270 例8-1过滤器代码与配置文件 略

  10. typora 使用

    菜单 输入+换行键,产生标题,自动更新 [toc] [TOC] 段落 按换行键建立新的一行可在行尾插入打断线,禁止向后插入 按换行键建立新的一行<br/> 标题 开头#的个数表示,空格+文 ...