Advertisement
jeff69

knnnn

Aug 7th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
4CS 1.09 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MINF -((1ll) << 61)
  6.  
  7. long long TC, n, a[1000][1000];
  8.  
  9. long long dp[1000][1000];
  10.  
  11. long long solve(int r, int c){
  12.     long long &ret = dp[r][c];
  13.     if (c < 0)
  14.         return MINF;
  15.     if (r == 2*n)
  16.         return 0;
  17.     if (a[r][c] == -1)
  18.         return MINF;
  19.     if (ret != -1)
  20.         return ret;
  21.     if (r < n)
  22.         return ret = max(solve(r + 1, c) + a[r][c], solve(r + 1, c + 1) + a[r][c]);
  23.     return ret = max(solve(r + 1, c - 1) + a[r][c], solve(r + 1, c) + a[r][c]);
  24. }
  25. int main(){
  26.     scanf("%lld", &TC);
  27.     int c = 1;
  28.    // freopen("out.txt","w",stdout);
  29.     while (TC--){
  30.  
  31.         memset(a, -1ll, sizeof a);
  32.         memset(dp, -1ll, sizeof dp);
  33.         scanf("%lld", &n);
  34.         for (int i=1;i<=n;i++)
  35.             for (int j=0;j<i;j++)
  36.                 scanf("%lld", &a[i][j]);
  37.         for (int i=n + 1;i<= 2*n - 1;i++)
  38.             for (int j=0;j<2*n - i;j++)
  39.                 scanf("%lld", &a[i][j]);
  40.         printf("Case %d: %lld", c++, solve(1, 0));
  41.              printf("\n");
  42.     }
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement