Codeforces 430D Working out【dp】好题~

D. 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 matrixa with n lines andm columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in thei-th line and the j-th column.

Iahub starts with workout located at line 1 and column1. 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 workouta[n][1] and she needs to finish with workouta[1][m]. After finishing workout from cella[i][j], she goes to eithera[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 andm (3 ≤ n, m ≤ 1000). Each of the nextn lines contains m integers:j-th number from i-th line denotes elementa[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 exercisesa[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].


题目大意:

一个人从左上走到右下,一个人从左下走到右上,两个人必须有一个点作为见面点,见面点的权值不能拿,问按照规则走,取得最大权值的和为多少。


思路:


1、做这种题肯定是着重考虑这个见面点,对于见面点(X,Y):


其对于第一个人只能从其上边或者左边过来,同理,对于第二个人,只能从下边或者是左边过来,那么无非对于这个图只能有两种情况:


绿色块表示第一个人可以走的两个区域,蓝色表示第二人可以走的两个区域。


2、那么很容易想到(对应两种情况都要处理):

output=max(两个绿色区域的最大值+两个蓝色区域的最大值);

那么我们预处理出4个区域的走法的最大值,接下来对于两种情况分别讨论即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
ll a[1005][1005];
ll dp[1005][1005];
ll dp2[1005][1005];
ll dp3[1005][1005];
ll dp4[1005][1005];
int n,m;
void init()
{
    memset(dp,0,sizeof(dp));
    memset(dp2,0,sizeof(dp2));
    memset(dp3,0,sizeof(dp3));
    memset(dp4,0,sizeof(dp4));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            dp[i][j]=max(dp[i][j-1],dp[i-1][j])+a[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=m;j>=1;j--)
        {
            dp2[i][j]=max(dp2[i][j+1],dp2[i-1][j])+a[i][j];
        }
    }
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;j++)
        {
            dp3[i][j]=max(dp3[i][j-1],dp3[i+1][j])+a[i][j];
        }
    }
    for(int i=n;i>=1;i--)
    {
        for(int j=m;j>=1;j--)
        {
            dp4[i][j]=max(dp4[i][j+1],dp4[i+1][j])+a[i][j];
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%I64d",&a[i][j]);
            }
        }
        ll output=0;
        init();
        for(int i=2;i<n;i++)
        {
            for(int j=2;j<m;j++)
            {
                output=max(output,((dp[i-1][j]+dp4[i+1][j])+(dp3[i][j-1]+dp2[i][j+1])));
                output=max(output,((dp[i][j-1]+dp4[i][j+1])+(dp3[i+1][j]+dp2[i-1][j])));
            }
        }
        printf("%I64d\n",output);
    }
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值