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 ;
}

cf 429B Working out(简单dp)的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  3. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  8. hdu 2471 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=(  dp[n-1][m],dp[n][m-1],d[i][k ...

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

  10. poj1189 简单dp

    http://poj.org/problem?id=1189 Description 有一个三角形木板,竖直立放.上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周 ...

随机推荐

  1. HDFS源码分析数据块复制选取复制源节点

    数据块的复制当然需要一个源数据节点,从其上拷贝数据块至目标数据节点.那么数据块复制是如何选取复制源节点的呢?本文我们将针对这一问题进行研究. 在BlockManager中,chooseSourceDa ...

  2. 【LeetCode-面试算法经典-Java实现】【118-Pascal&#39;s Triangle(帕斯卡三角形)】

    [118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...

  3. dede后台title怎么修改的?去掉XXXX-织梦内容管理系统V5.7

    dede后台title怎么修改的? 去掉XXXX-织梦内容管理系统V5.7 打开include/common.inc.php的文件. $cfg_version = 'V57_UTF8_SP1';(这是 ...

  4. python selenium - SSL处理(https)

    在实际的自动化测试实践中,因为越来越多的站点接入https,使得我们原有的python selenium2自动化测试代码进行测试时,浏览器总是报安全问题,即便在浏览器选项中将被测网址加入信任网址也没用 ...

  5. How can I detect multiple logins into a Django web application from different locations?

    1) Install django-tracking (thankyou for that tip Van Gale Google Maps + GeoIP is amazing!) 2) Add t ...

  6. Array的push与unshift方法性能比较分析

    从原理就可以知道,unshift的效率是较低的.原因是,它每添加一个元素,都要把现有元素往下移一个位置.但到底效率差异有多大呢?下面来测试一下. 测试环境的主要硬件:CPU T7100(1.8G):内 ...

  7. [原创]实现多层DIV叠加的js事件穿透

    Flash里面有个很好的特性是,一个容器里,不存在实际对象的部分,不会阻拦鼠标事件穿透到下一层. 前端就不一样了,两个div层叠以后,上层div会接收到所有事件(即使这个div里面内容是空的,没有任何 ...

  8. Spring MVC的视图解析器

    一.视图解析器简介 在Spring MVC中,当Controller将请求处理结果放入到ModelAndView中以后,DispatcherServlet会根据ModelAndView选择合适的视图进 ...

  9. thinkphp5, 省略index.php

    Apache:1. httpd.conf配置文件中加载了mod_rewrite.so模块2. AllowOverride None 将None改为 All3. 把下面的内容保存为.htaccess文件 ...

  10. Linux根目录下重要文件夹

    bin存放系统命令的目录,普通用户和超级用户都可以执行,不过放在这里的命令在单用户模式下也可以执行 sbin保存和系统环境设置相关的命令,只有超级用户可以使用这些命令进行系统环境的设置,但有些命令可以 ...