B. Working out
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
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].

给一个n*m的方格,A从左上角走到右下角,B从左下角走到右上角,路线交叉处的权值不算,问两条路线权值之和最大值。要求:两条路线只在一点交叉。

可以枚举交叉点,求该点到四个角落的权值之和。到每个角的权值都可以dp得到最大值。从左上角顺时针得到0,1,2,3四个方向。

 
两幅图的权值之和分别是: 
dp[i][j-1][0] + dp[i-1][j][1] + dp[i][j+1][2] + dp[i+1][j][3] 
dp[i][j-1][3] + dp[i-1][j][0] + dp[i][j+1][1] + dp[i+1][j][2]

还有需注意的是,交叉点只在(n-2)*(m-2)里面这个矩形里变化(否则一个角上的矩形会不存在)

 #include <bits/stdc++.h>
using namespace std; const int MAXN = ;
__int64 a[MAXN][MAXN];
__int64 dp[MAXN][MAXN][];//0, 1, 2, 3分别代表左上,右上,右下,左下 int main()
{ int n, m;
int i, j; while (~scanf("%d%d", &n, &m)) {
for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
scanf("%I64d", &a[i][j]);
}
}
memset(dp, , sizeof(dp)); for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
dp[i][j][] = max(dp[i - ][j][], dp[i][j - ][]) + a[i][j];
}
}
for (i = ; i <= n; ++i) {
for (j = m; j >= ; --j) {
dp[i][j][] = max(dp[i - ][j][], dp[i][j + ][]) + a[i][j];
}
}
for (i = n; i >= ; --i) {
for (j = m; j >= ; --j) {
dp[i][j][] = max(dp[i + ][j][], dp[i][j + ][]) + a[i][j];
}
}
for (i = n; i >= ; --i) {
for (j = ; j <= m; ++j) {
dp[i][j][] = max(dp[i + ][j][], dp[i][j - ][]) + a[i][j];
}
} __int64 ans = ;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
ans = max(ans, dp[i][j - ][] + dp[i - ][j][] + dp[i][j + ][] + dp[i + ][j][]);
ans = max(ans, dp[i][j - ][] + dp[i - ][j][] + dp[i][j + ][] + dp[i + ][j][]);
}
}
printf("%I64d\n", ans);
} return ;
}

最新文章

  1. ajax提交数据到java后台,并且返回json格式数据前台接收处理值
  2. PHP 异常
  3. CEF3开发者系列之类和接口
  4. PHP5.3中关于VC9和VC6以及Thread Safe和Non Thread Safe版本选择的问题
  5. IE6下z-index失效
  6. C#_delegate - example
  7. [NOIP2011]数的划分
  8. PHP操作Oracle数据库
  9. android隐式intent使用场景解析
  10. Android消息推送之GCM方式(一)
  11. xx-net 使用方式
  12. Dapper源码学习和源码修改(下篇)
  13. 关于$(function(){})的问题
  14. eclipse添加market ,maven
  15. JS的分号可以省掉吗?
  16. HTTP基础知识3
  17. elasticsearch(三) 之 elasticsearch目录介绍和配置文件详解
  18. Gitlab Issue Tracker and Wiki(二)
  19. 将asi-http-request引入到ARC工程需要做的 转
  20. BroadcastReceiver之实现锁屏、解锁样例

热门文章

  1. Android Studio 使用笔记:查看类结构和继承关系
  2. xml初学简单介绍
  3. xfs 文件系统损坏修复 fscheck
  4. Spring WebSocket Support官方文档+翻译
  5. HTTP Status Codes 状态码
  6. ListView中加载大量的图片
  7. Live555 中的客户端openRTSP 保存H264文件
  8. C# .Net 下 x86使用大内存的处理
  9. (比赛)B - 棋盘问题(dfs)
  10. EasyNVR无插件摄像机直播之:摄像机网页低延时无插件直播实现