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. SpringCloud学习系列-Eureka服务注册与发现(2)

    构建 microservicecloud-eureka-7001 eureka服务注册中心Module 1.新建microservicecloud-eureka-7001 2.pom <proj ...

  2. Python 爬虫十六式 - 第五式:BeautifulSoup-美味的汤

    BeautifulSoup 美味的汤 学习一时爽,一直学习一直爽!    Hello,大家好,我是Connor,一个从无到有的技术小白.上一次我们说到了 Xpath 的使用方法.Xpath 我觉得还是 ...

  3. PHP+FLASH大文件断点续传功能分享

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  4. css使用2

    一.盒子模型 盒子模型 margin:用来调节盒子与盒子之间的距离(标签与标签之间距离) border:盒子的包装厚度(边框) padding:内部物体与盒子之间距离(文本与边框之间的距离) cont ...

  5. 在Eclipse中搭建Android开发环境

    忙活了两天多的时间,终于在Eclipse中成功搭建了Android开发环境,着实不易啊!! 原本我用的编译器是MyEclipse的,但是从网上找不到如何在MyEclipse中搭建环境,于是乎就换了Ec ...

  6. BZOJ 3168 Luogu P4100 [HEOI2013]钙铁锌硒维生素 (矩阵求逆、二分图匹配)

    线性代数+图论好题. 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3168 (luogu) https://www.lu ...

  7. 大哥带的Orchel数据库的注入

    0X01 先进行判断 a.jsp?username=SMITH and = 发现单引号闭合  我们尝试构造闭合  存在注入 a.jsp?username=SMITH'='1 正确 a.jsp?user ...

  8. kafka-manager怎么查看topic里的数据量

    https://jingyan.baidu.com/article/eb9f7b6d367679869364e8d4.html

  9. C语言的AES加密

    C语言的AES加密 稍微封装了几个函数 方便使用 #if 1 #include <stdio.h> #include <stdlib.h> #include <strin ...

  10. iOS堆栈内存区别

    堆和栈的区别: · 1> 堆空间的内存是动态分配的,一般存放对象,并且需要手动释放内存. · 2> 栈空间的内存由系统自动分配,一般存放局部变量等,不需要手动管理内存. 接下来我将从以下几 ...