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. 【xlwings1】多线程写入excel数据

    #!/ufr/bin/env python # -*- coding:utf-8 -*- import xlwings as xw import queue import threading impo ...

  2. 读书笔记--Head First Ajax 目录

    1.使用Ajax 2.设计Ajax 3.javascripte事件 4.多个事件处理程序 5.异步应用 6.文档对象模型 7.管理DOM 8.框架与工具包 9.xml请求与响应 10.json 11. ...

  3. mybatis深入理解(四)-----MyBatis的架构设计以及实例分析

    MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构设计思路,并且讨论MyBatis的几个核心部件,然后结合一个select查询实例, ...

  4. win10 基础上装一个 ubuntu 双系统

    1.准备一个空闲的分区: 1)确定每个磁盘都已经是 EFI 分区格式,如果不是,可以使用分区工具,将分区都转成EFI  (例如 DiskGenius 工具挺好) 2)选择一个剩余空间较大的压缩空间,压 ...

  5. Git--版本管理的使用及理解

    如果多人合作时,git也是需要中间交换服务器来解决冲突合并,这不还是集中式版本控制吗? 而svn不是也可以将所有源码下载到本机,然后在本机修改,保存在本机上,为什么这个不能说是分布式,提交的时候不也是 ...

  6. Constructing Roads POJ - 2421 (最小生成树)

    思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将    所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...

  7. 获得浏览器User-agent的方法

    在浏览器的地址栏输入(不是全部都能用) javascript:alert(navigator.userAgent); 或者网页中 alert(navigator.userAgent) 或者后台中 St ...

  8. Redis数据库在ubuntu16.04下的安装

    1.安装 sudo apt-get install redis-server 2.启动 sudo service redis-server start 3.查看 ps aux|grep redis 4 ...

  9. day 56

    目录 聚合查询 分组查询 F与Q查询 ORM字段及参数 13个字段操作总结 自定义char字段 ORM中事物的操作 数据库三大范式 聚合查询 aggregate()是QuerySet()的一个终止子句 ...

  10. day38 04-Spring加载配置文件

    Spring的工厂类ApplicationContext有两个子类:ClassPathXmlApplicationConext和FileSystemXmlApplication都可以用来加载配置文件. ...