CF429B Working out(动态规划,四角dp)

Virtual Judge-CF429B
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].

题意

两个人A和B要在健身房健身,给出一个n行m列的矩阵,表示在每个房间消耗的卡路里的数量。

A只能从(1,1)点到(n,m),在a[i][j]完成健身之后,他只能走到a[i + 1][j] 或者 a[i][j + 1],B只能从(n,1)到(1,m),在a[i][j]完成健身之后,他只能走到a[i][j+1] 或者 a[i-1][j]。

两人如果在某一个房间见面了,那个在这个房间都不进行训练,也就不消耗卡路里。

两人的速度可以不同,也就是说两人在相遇前后经过的房间数量不一定相同。

问两人一共消耗卡路里的最大数量。

理解

首先这个一个和HDU-2084 数塔很像的一个题,到某个点的最优值用一个简单的dp递推式就可以达到,比如从左上到右下的每个点的最优值可以表示为:dp[i][j] = a[i][j] + max(dp[i-1][j],dp[i][j-1])。由此可以推出其他三个方向的递推式。

假设两人在(x,y)点相遇,那么如果A是从左边走过来的,那么B一定是从下方走过来的,道理很简单:如果B也是从左边走过来的,那么就已经在上一个房间相遇了,为什么还要同时走向(x,y)点。
所以只有两种情况:
1)A从左边经过(x,y)点,然后接着向右走,B从下方经过(x,y)点,然后接着向上走。
2)A从上边经过(x,y)点,然后接着向下走,B从左方经过(x,y)点,然后接着向右走。

那么最后用n^2的时间枚举每个非边界点作为相遇点的消耗,算出最大值,即是答案。

代码

#include <bits/stdc++.h>
using namespace std;

int a[1010][1010];
int dp1[1010][1010
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值