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
*/

水博客时间结束。

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

Codeforces 429 B. Working out-dp( Codeforces Round #245 (Div. 1))的更多相关文章

  1. DP BestCoder Round #50 (div.2) 1003 The mook jong

    题目传送门 /* DP:这题赤裸裸的dp,dp[i][1/0]表示第i块板放木桩和不放木桩的方案数.状态转移方程: dp[i][1] = dp[i-3][1] + dp[i-3][0] + 1; dp ...

  2. Codeforces Round #245 (Div. 1) B. Working out (简单DP)

    题目链接:http://codeforces.com/problemset/problem/429/B 给你一个矩阵,一个人从(1, 1) ->(n, m),只能向下或者向右: 一个人从(n, ...

  3. Codeforces Round #245 (Div. 1) B. Working out (dp)

    题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...

  4. Codeforces Round #245 (Div. 1) B. Working out dp

    题目链接: http://codeforces.com/contest/429/problem/B B. Working out time limit per test2 secondsmemory ...

  5. codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。

    限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...

  6. Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对

    D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...

  7. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  8. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  9. Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心

    A. Points and Segments (easy) Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

随机推荐

  1. Android 自定义ListView实现底部分页刷新与顶部下拉刷新,androidlistview

    在项目开发中,由于数据过大时,需要进行分页加载或下拉刷新,来缓解一次性加载的过长等待.本篇博文实例讲解通过自定义的ListView实现底部分页加载和顶部下拉刷新的效果. 其效果图: 一.ListVie ...

  2. 2015/9/1 Python基础(6):列表

    列表和字符串类型很相似,是同样的序列式数据类型.但是字符串只能由字符组成,列表可以保留任意数目的Python对象的灵活的容器.Python的列表比C的数组要灵活,数组里面只能是一种类型,列表可以有多种 ...

  3. 【BZOJ】1299: [LLH邀请赛]巧克力棒

    [算法]博弈论 [题解]这道题不是典型的SG函数题了. 不把它当成游戏看待,那么这道题是在说n个石子堆,每次可以加入若干个或进行Nim游戏. 我们当前先手,则考虑构造必败态来获胜. 当前已加入的NIm ...

  4. 【BZOJ】3790 神奇项链

    [算法](manacher+贪心)||(manacher+DP+树状数组/线段树) [题解] manacher求回文串,后得到线段,做一点计算映射回原串线段. 然后问题转化为可重叠区间线段覆盖问题,可 ...

  5. javascript 事件绑定

    一.最简单和向后兼容性最好的事件绑定方法是把事件绑定到元素标识的属性.事件属性名称由事件类型外加一个“on”前缀构成.这些属性也被称为事件处理器 <INPUT TYPE="text&q ...

  6. python碎片记录(二)

    1.字典中嵌套字典使用 dict={'a':{1:2,2:3}} print(dict) print(dict['a'][2]) 输出如下: {'a': {1: 2, 2: 3}} 3  2.元组与l ...

  7. bootstrap-select属性

    # 参考:https://blog.csdn.net/zxl_langya/article/details/79247307 # bootstrap-select属性: <select mult ...

  8. 基于Django Form源码开发自定义Form组件

    import copy import re class ValidateError(Exception): def __init__(self, detail): self.detail = deta ...

  9. Low overhead memory space management

    Methods, apparatus, and systems, including computer programs encoded on a computer storage medium, m ...

  10. python基础===成员访问__len__()和__getitem__()

    class A: def __init__(self,*args): self.name = arg pass def __len__(self): return len(self.name) a = ...