time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 m columns. 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.

Examples
input

Copy
3 3
100 100 100
100 1 100
100 100 100
output

Copy
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四次,分别从左上角走到右下角,左下角走到右上角,右上角走到左下角,右下角走到左上角。

然后考虑两人相遇的情况,只有两种满足,一种是右上,一种是下右,相遇之后也是这种状态。

然后枚举点,边界处是不满足的,去掉。

代码:

 //Codeforces 429 B. Working out-dp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e3+;
const int maxnm=1e5+; ll dp1[maxn][maxn],dp2[maxn][maxn],dp3[maxn][maxn],dp4[maxn][maxn]; int main()
{
int n,m;
scanf("%d%d",&n,&m);
int a[n+][m+];
for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
scanf("%d",&a[i][j]);
}
for(int i=;i<=n;i++){//(1,1)->(n,m)
for(int j=;j<=m;j++){
dp1[i][j]=max(dp1[i-][j],dp1[i][j-])+a[i][j];
}
}
for(int i=n;i>;i--){//(n,1)->(1,m)
for(int j=;j<=m;j++){
dp2[i][j]=max(dp2[i][j-],dp2[i+][j])+a[i][j];
}
}
for(int i=n;i>;i--){//(n,m)->(1,1)
for(int j=m;j>;j--){
dp3[i][j]=max(dp3[i][j+],dp3[i+][j])+a[i][j];
}
}
for(int i=;i<=n;i++){//(1,m)->(n,1)
for(int j=m;j>;j--){
dp4[i][j]=max(dp4[i][j+],dp4[i-][j])+a[i][j];
}
}
ll ans=;
int posx,posy,flag=;
for(int i=;i<n;i++){//枚举点,然后两种情况
for(int j=;j<m;j++){//去掉边界,边界不满足情况,就可以了。
ll cnt=dp1[i][j-]+dp3[i][j+]+dp2[i+][j]+dp4[i-][j];//右上
ll ret=dp1[i-][j]+dp3[i+][j]+dp2[i][j-]+dp4[i][j+];//下右
ans=max(ans,max(cnt,ret));
}
}
printf("%lld\n",ans);
} /*
3 3
3 1 2
3 2 0
2 3 2
*/

水博客时间结束。

最近不想写博客,不好玩。

最新文章

  1. css之absolute绝对定位(技巧篇)
  2. 第2/24周 页_SQL Server 中数据存储的基本单位
  3. ELK日志管理之——elasticsearch部署
  4. 基于HTML5实现的Heatmap热图3D应用
  5. XmlWriter/XmlReader示例代码
  6. extjs 远程数据源
  7. 使用Wireshark捕捉USB通信数据
  8. Javascript 补位运算符
  9. [Golang]使用自建代理访问指定网站
  10. 【Android】Handler使用入门
  11. RESTful服务的版本管理经验 (转)
  12. Web APP 随笔
  13. 正式软件工作第一天————MVC、ext JS、和clsa
  14. [Linux] Desktop Management
  15. Dreamweaver cs6中文版完整安装步骤:
  16. iOS &quot;此证书由未知颁发机构签名&quot;此问题的解决方法
  17. 简单http文件服务器
  18. 16.1 解决SecureCRT的Home+End+Del不好用使用方法
  19. 五句话搞定JavaScript作用域(ES5)
  20. Java复习总结——数据类型

热门文章

  1. 解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误
  2. [技巧篇]21.Android Studio的快捷键设置[图片版]
  3. Java设计模式の观察者模式(推拉模型)
  4. 这个随笔用用来放一些好的思想和思考方式(暂时secret)
  5. 数据结构:Bitset
  6. LightOJ 1151 - Snakes and Ladders 高斯消元+概率DP
  7. 【NOIP】提高组2016 天天爱跑步
  8. 【51NOD-0】1089 最长回文子串 V2(Manacher算法)
  9. Java并发——关键字synchronized解析
  10. Mayor&#39;s posters(线段树+离散化+区间染色)