Gym - 102448 K Kongey Donk (DP)

Description

The monkey Kongey Donk loves to eat bananas, but he is always really tired. Kongey lives in a region that has n banana trees lined in a row, and he just woke up over a platform from which he can jump to the top of any of the banana trees in the region. The banana tree in nlogonia(region where Kongey lives) have a peculiar characteristic in which there are only bananas in fixed points of the tree with distance of 1 meter between them. Kongey needs to go to the floor(the lowest position of the tree), but in his way down he wants to take as much bananas as possible.

Kongey starts his journey from the platform and he can jump to the top of any tree. When he is in a tree, he can jump to any adjacent tree or jump to change his position in the tree he is now. But, as he is really tired, when performing a jump he will always go to a position below the one he was before.

Before he starts his journey, Kongey asks what is the maximum number of bananas he can take considering he can carry an unlimited number of them.

Input

The first line of the input consists of 2 numbers n and h ( 0 < n , h ≤ 2 ∗ 1 e 5 , 0 < n ∗ h ≤ 1 e 6 ) (0<n,h≤2∗1e5, 0<n∗h≤1e6) (0<n,h21e5,0<nh1e6), the number of banana trees and the height of each of them, respectively.

Each of the following n lines describes a banana tree.

The i-th of them contains h non negative integer numbers describing how many bananas there are in each position of the i-th tree from top to bottom.

The number of bananas in each position is always less than or equal to 1 e 6 1e6 1e6.

Output

The output consists of a single integer number describing the maximum number of bananas Kongey can take.

Examples

Input
3 3
1 5 5
9 0 0
15 2 1
Output
20

Input
5 6
1 100 2 8 9 8
10 55 30 2 2 2
30 10 200 10 3 100
50 0 1 2 3 9
1 130 9 29 3 40

Output
398

Solution 简单DP

Code:

#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 7;
vector<ll>val[MX];
vector<ll>dp[MX];
int main(){
    int n,h;scanf("%d %d",&n,&h);//n cols h rows
    for(int j = 0;j < n;++j){
        for(int i = 0;i < h;++i){
            ll tmp;scanf("%lld",&tmp); 
            val[i].pb(tmp);
            dp[i].pb(0);
        }
    }
    ll res = 0;
    for(int j = 0;j < n;++j){
        dp[0][j] = val[0][j];
        res = max(res,dp[0][j]);
    }
    for(int i = 1;i < h;++i){
        for(int j = 0;j < n;++j){
            dp[i][j] = max(dp[i-1][max(0,j-1)], max(dp[i-1][j], dp[i-1][min(n-1,j+1)])) + val[i][j];
            res = max(res,dp[i][j]);
        }
    }
    printf("%lld\n", res);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值