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

Example

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

#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = + ;
int mat[N][N], a[N][N], b[N][N], c[N][N], d[N][N]; int main(){
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) scanf("%d", &mat[i][j]); for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) a[i][j] = max(a[i-][j], a[i][j-]) + mat[i][j]; for(int i = n; i > ; i--)
for(int j = m; j > ; j--) b[i][j] = max(b[i+][j], b[i][j+]) + mat[i][j]; for(int i = n; i > ; i--)
for(int j = ; j <= m; j++) c[i][j] = max(c[i+][j], c[i][j-]) + mat[i][j]; for(int i = ; i <= n; i++)
for(int j = m; j > ; j--) d[i][j] = max(d[i-][j], d[i][j+]) + mat[i][j];
int ans = ;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
ans = max(ans, a[i-][j] + b[i+][j] + c[i][j-] + d[i][j+]),
ans = max(ans, a[i][j-] + b[i][j+] + c[i+][j] + d[i-][j]);
printf("%d\n", ans);
}

数塔 Medium的更多相关文章

  1. 数塔问题(DP算法)自底向上计算最大值

    Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数 ...

  2. dp入门--poj 1163数塔

                                                                                                        ...

  3. ACM 杭电HDU 2084 数塔 [解题报告]

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  4. [ACM_动态规划] 数字三角形(数塔)

    递归方法解决数塔问题 状态转移方程:d[i][j]=a[i][j]+max{d[i+1][j],d[i+1][j+1]} 注意:1\d[i][j]表示从i,j出发的最大总和;2\变界值设为0;3\递归 ...

  5. HDU-2084 数塔 经典dp,水

    1.HDU-2084   数塔 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 3.总结:从下往上推,最后归于顶点.方程为  dp[i][j] ...

  6. HDU2084基础DP数塔

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  7. hdu----(2084)数塔(dp)

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  8. HDU 2084 数塔(动态规划)

    数塔 http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描 ...

  9. hdu 2084 数塔 (简单dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=2084 数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory L ...

随机推荐

  1. ubuntu 7z解压

    安装方法:     sudo apt-get install p7zip 解压文件:     7z x manager.7z -r -o /home/xx 解释如下: x 代表解压缩文件,并且是按原始 ...

  2. CF718C Sasha and Array 线段树 + 矩阵乘法

    有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$   直接求不好求,改成矩阵乘法的形式:  $a_{i}=M^x\times ...

  3. python3学习笔记(二):Python初识

    一.算法 在开始认真地编程之前,首先来解释下什么是计算机程序设计.简单地说,它就是告诉计算机要做什么.计算机可以做很多事情,但是它不会自己思考,需要我们告诉它具体细节,并且使用计算机能够理解的语言把算 ...

  4. js控制手机保持亮屏的库,解决h5移动端,自动息屏问题

    一些说明:我用Laya(ts)开发小游戏,有需要保持手机屏幕常亮的需求(非必须的),然后作为小白的我就在网上找到了这个库,大概了解下,应该是通过播放空视频的原理来保持手机屏幕常亮,然后就放到项目中试了 ...

  5. JAVA第二周课程总结

    本周我们开始学习一门新的课程JAVA 本周主要学习内容: 1.认识java,以及它的开发工具jdk 2.了解Java的语言特点 3.搭建Java开发环境,jdk的安装和配置

  6. SRCNN 卷积神经网络

    2019-05-19 从GitHub下载了代码(这里) 代码量虽然不多,但是第一次学,花了时间还是挺多的.根据代码有跑出结果(基本没有改),但是对于数据集的处理还是看的很懵逼,主要是作者的实现都是用类 ...

  7. 使用HeapAnalyzer分析内存泄漏

    从IBM网站下载ha433包,释放,执行ha433.jar文件 https://www.ibm.com/developerworks/mydeveloperworks/groups/service/h ...

  8. NDK下编译JNI

    NDK环境下编译JNI 下载demo.tar.gz然后解压 弄个套路 1.编辑build.sh设置好NDK目录 2.把cpp文件放到code下面 运行sh build.sh即可

  9. leetcode-mid-sorting and searching - 240. Search a 2D Matrix II -NO

    mycode   time limited def searchMatrix(matrix, target): def deal(data): if not data: return False ro ...

  10. Spring 缓存切面

    缓存切面:[通知+目标方法调用] 缓存操作执行过程: 1)如果是同步调用[sync=true],则首先尝试从缓存中读取数据,读取到则直接返回: 否则执行目标方法,将结果缓存后返回. 2)如果不是同步调 ...