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

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].

题目意思:

给n*m的矩阵,每个格子有个数,A从(1,1)出发只能向下或右走,终点为(n,m),B从(n,1)出发只能向上或右走,终点为(1,m)。两个人的速度不一样,走到的格子可以获的该格子的数,两人相遇的格子上的数两个人都不能拿。求A和B能拿到的数的总和的最大值。

n,m<=1000

解题思路:

dp.

先预处理出每个格子到四个角落格子的路径最大数值,然后枚举两个人相遇的交点格子,枚举A、B的进来和出去方式,求最大值即可。

注意边界情况。

#include<iostream>
#include<string.h>
#include<string>
#include<cmath>
using namespace std; typedef long long ll;
const int N = 1e3+;
int n,m;
ll f1[N][N], f2[N][N], f3[N][N], f4[N][N];
ll a[N][N]; ll getRes(int i,int j) {
return max( f1[i][j-] + f4[i][j+] + f2[i+][j] + f3[i-][j]
, f1[i-][j] + f4[i+][j] + f2[i][j-] + f3[i][j+]);
} int main () {
//freopen("./Desktop/in.txt","r",stdin);
scanf("%d %d", &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++)
f1[i][j] = max(f1[i-][j], f1[i][j-]) + a[i][j];
for(int i=n;i>=;i--)
for(int j=;j<=m;j++)
f2[i][j] = max(f2[i][j-], f2[i+][j]) + a[i][j];
for(int i=;i<=n;i++)
for(int j=m;j>=;j--)
f3[i][j] = max(f3[i-][j], f3[i][j+]) + a[i][j];
for(int i=n;i>=;i--)
for(int j=m;j>=;j--)
f4[i][j] = max(f4[i+][j], f4[i][j+]) + a[i][j];
ll mx=;
for(int i=;i<n;i++) {
for(int j=;j<m;j++) {
ll res = getRes(i,j);
mx = max(mx, res);
//printf("%lld ",res);
}//puts("");
}
cout << mx <<endl;
return ;
}

cf 429 B Working out的更多相关文章

  1. 【CF Round 429 B. Godsend】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  3. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  4. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  5. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  6. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  7. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  8. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  9. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

随机推荐

  1. JavaIO再回顾

    File类 JavaIO访问文件名和文件检测相关操作 分隔符最好是使用File类提供的File.separator,使程序更加的健壮. File类提供的方法基本上是见名知意,例如getName()就是 ...

  2. java的this表示当前类还是当前实例?

    转自:http://www.runoob.com/java/java-basic-syntax.html this 表示调用当前实例或者调用另一个构造函数

  3. web站点检查简易shell脚本

    1.web样式 <h4>THE STATUS OF RS:</h4> <meta http-equiv="> <table border=" ...

  4. vscode 代码保存时自动格式化成 ESLint 风格

    vscode 的默认的代码格式化 alt+shift+f (windows) 无法通过 eslint 的代码风格检查,,, 比如: 4个空格和2个空格(ESLint) 字符串用单引号(ESLint) ...

  5. poj1185 炮兵阵地【状压DP】

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32802   Accepted: 12650 Descriptio ...

  6. SQL---->mySQl卸载for mac

    因为装的时候弄坏了 先来学习下怎么卸载吧,如下输入终端就好了 cd ~/ sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm ...

  7. Oracle等待事件之Latch Free

    1.产生原因 表示某个锁存器上发生了竞争.首先应该确保已经提供了足够多的Latch 数,如果仍然发生这种等待事件,那么应该进一步确定是那种锁存器上发生了竞争(在v$session_wait 上的P2 ...

  8. Dolls---hdu4160(最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4160 有n个长方体形的娃娃:当长宽高都小于另一个的时候可以放进去,每一个里面最多放一个,问最优的套法下 ...

  9. EOS token 代币兑换的资料

    eos token 兑换价格预估查询:   https://eosscan.io/ https://steemit.com/eos/@sandwich/how-to-check-which-eos-p ...

  10. 【开发者笔记】揣摩Spring-ioc初探,ioc是不是单例?

    前言: 控制反转(Inversion of Control,英文缩写为IoC)把创建对象的权利交给框架,是框架的重要特征,并非面向对象编程的专用术语.它包括依赖注入(Dependency Inject ...