Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define MINF -((1ll) << 61)
- long long TC, n, a[1000][1000];
- long long dp[1000][1000];
- long long solve(int r, int c){
- long long &ret = dp[r][c];
- if (c < 0)
- return MINF;
- if (r == 2*n)
- return 0;
- if (a[r][c] == -1)
- return MINF;
- if (ret != -1)
- return ret;
- if (r < n)
- return ret = max(solve(r + 1, c) + a[r][c], solve(r + 1, c + 1) + a[r][c]);
- return ret = max(solve(r + 1, c - 1) + a[r][c], solve(r + 1, c) + a[r][c]);
- }
- int main(){
- scanf("%lld", &TC);
- int c = 1;
- // freopen("out.txt","w",stdout);
- while (TC--){
- memset(a, -1ll, sizeof a);
- memset(dp, -1ll, sizeof dp);
- scanf("%lld", &n);
- for (int i=1;i<=n;i++)
- for (int j=0;j<i;j++)
- scanf("%lld", &a[i][j]);
- for (int i=n + 1;i<= 2*n - 1;i++)
- for (int j=0;j<2*n - i;j++)
- scanf("%lld", &a[i][j]);
- printf("Case %d: %lld", c++, solve(1, 0));
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement