数塔 Medium

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 3100 100 100100 1 100100 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 = 1000 + 5;
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 = 1; i <= n; i++)
        for(int j = 1; j <= m; j++) scanf("%d", &mat[i][j]);

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++) a[i][j] = max(a[i-1][j], a[i][j-1]) + mat[i][j];

    for(int i = n; i > 0; i--)
        for(int j = m; j > 0; j--)  b[i][j] = max(b[i+1][j], b[i][j+1]) + mat[i][j];

    for(int i = n; i > 0; i--)
        for(int j = 1; j <= m; j++) c[i][j] = max(c[i+1][j], c[i][j-1]) + mat[i][j];

    for(int i = 1; i <= n; i++)
        for(int j = m; j > 0; j--)  d[i][j] = max(d[i-1][j], d[i][j+1]) + mat[i][j];
    int ans = 0;
    for(int i = 2; i < n; i++)
        for(int j = 2; j < m; j++)
            ans = max(ans, a[i-1][j] + b[i+1][j] + c[i][j-1] + d[i][j+1]),
            ans = max(ans, a[i][j-1] + b[i][j+1] + c[i+1][j] + d[i-1][j]);
    printf("%d\n", ans);
}
时间: 2024-10-10 14:41:43

数塔 Medium的相关文章

数塔 Easy

在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?  已经告诉你了,这是个DP的题目,你能AC吗? Input输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内. Output对于每个测试实例,输出可能得到的最大和,每

1002 数塔取数问题

1002 数塔取数问题 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值. 每次只能走到下一层相邻的数上,例如从第3层的6向下走,只能走到第4层的2或9上. 5 8 4 3 6 9 7 2 9 5 例子中的最优方案是:5 + 8 + 6 + 9 = 28 Input 第1行:N,N为数塔的高度.(2 <= N <= 500) 第2 - N + 1行:每行包括1层数塔的数字,第2行1个数,第3

数塔问题

题目:给定一个数塔.其存储形式为例如以下所看到的的下三角矩阵.在此数塔中,从顶部出发,在每一节点能够选择向下走还是向右走,一直走究竟层.请找出一条路径,使路径上的数值和最大. 输入例子(数塔): 9 12   15 10   6    8 2    18   9    5 19   7    10   4    16 输出例子(最大路径和): 59 分析: 这是动态规划的入门级题目,思路非常easy,自下向上思考,相邻两个数比較大小.取较大的数加到上一层相应的数上面.这样就能够消掉最以下一层,重

HDU 2084 数塔(简单DP入门)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 41852    Accepted Submission(s): 24820 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

HDU 2084 数塔

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 32087    Accepted Submission(s): 19162 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

hdu2084 数塔 动态规划第二题

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 25898    Accepted Submission(s): 15621 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少

HDOJ1176 免费馅饼 【DP】+【经典数塔】

免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23986    Accepted Submission(s): 8093 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的1

HDU2084_数塔【简单题】【数塔】

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 22488    Accepted Submission(s): 13555 Problem Description 在讲述DP算法的时候,一个经典的样例就是数塔问题,它是这样描写叙述的: 有例如以下所看到的的数塔.要求从顶层走究竟层,若每一步仅仅能走到相邻的结点,则经过的结点的数字

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

Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内. Output 对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行. Sample Input 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 Sample Output 30 1 #include<iostream> 2 #incl