DuongNhi99

F - Monkey Banana Problem (24-11)

Dec 10th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. const int N = 10005;
  6.  
  7. ll a[205][105];
  8. ll dp[205][105];
  9.  
  10. ll solve(ll n, ll k) {
  11.  
  12.     memset(dp, 0, sizeof(dp));
  13.  
  14.     for(int i = 1; i <= n; ++i)
  15.         for(int j = 1; j <= i; ++j)
  16.             dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) + a[i][j];
  17.  
  18.     for(int i = n + 1; i <= k; ++i)
  19.         for(int j = 1; j <= n; ++j)
  20.             dp[i][j] = max(dp[i-1][j+1], dp[i-1][j]) + a[i][j];
  21.  
  22.     return dp[k][1];
  23. }
  24.  
  25. int main() {
  26.     //freopen("in.txt", "r", stdin);
  27.     //freopen("F-MonkeyBananaProblem.inp", "r", stdin);
  28.     //freopen("F-MonkeyBananaProblem.out", "w", stdout);
  29.     //ios_base::sync_with_stdio(false);
  30.     //cin.tie(NULL); cout.tie(NULL);
  31.  
  32.     int t; cin >> t;
  33.     for(int test = 1; test <= t; ++test){
  34.         ll n; cin >> n;
  35.         for(int i = 1; i <= n; ++i)
  36.             for(int j = 1; j <= i; ++j)
  37.                 cin >> a[i][j];
  38.  
  39.         ll k = 2*n - 1;
  40.         ll m = n;
  41.         for(int i = n + 1; i <= k; ++i) {
  42.             m--;
  43.             for(int j = 1; j <= m; ++j)
  44.                 cin >> a[i][j];
  45.         }
  46.         cout << "Case " << test << ": " << solve(n, k) << endl;
  47.     }
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment