K - Kongey Donk Gym - 102448K(dp)

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∗105, 0<n∗h≤106), 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 106.

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
Note
For all 0<i<n, the i-th banana tree and the (i−1)-th banana tree are adjacent.

In the first sample, the best path goes through trees: 3, 2, 1 getting respectively 15, 0, 5 bananas.

題意:
n棵樹,高度爲h。每個位置有一些香蕉,
從上面往下走,每次走一格,可以向本樹走,也可以走到相鄰樹下一格。
求最多收集多少香蕉。

思路:
有點像雷濤的小貓那道題
直接定義f[i][j]f[i][j]爲到了第ii棵樹高度爲jj的最大值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 7;

vector<vector<ll> >f;

int main() {
    int n,h;scanf("%d%d",&n,&h);
    f.resize(n + 2);
    f[0].resize(h + 2);
    f[n + 1].resize(h + 2);
    for(int i = 1;i <= n;i++) {
        f[i].resize(h + 2);
        for(int j = 1;j <= h;j++) {
            ll x;scanf("%lld",&x);
            f[i][j] = x;
        }
    }
    
    for(int j = h - 1;j >= 1;j--) {
        for(int i = 1;i <= n;i++) {
            f[i][j] += max(f[i - 1][j + 1],max(f[i][j + 1],f[i + 1][j + 1]));
        }
    }
    
    ll ans = 0;
    for(int i = 1;i <= n;i++) {
        ans = max(ans,f[i][1]);
    }
    
    printf("%lld\n",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章