Advertisement
Josif_tepe

Untitled

May 12th, 2025
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int n;
  6. int mat[101][101];
  7. int dp[101][101];
  8. const int INF = 1e8;
  9. int rec(int i, int j) {
  10.     if(i == n - 1) {
  11.         return mat[i][j];
  12.     }
  13.     if(dp[i][j] != -1) {
  14.         return dp[i][j];
  15.     }
  16.    
  17.     int res = -INF;
  18.     if(i + 1 < n and mat[i + 1][j] != -1) {
  19.         res = max(res, rec(i + 1, j) + mat[i][j]);
  20.     }
  21.     if(i + 1 < n and j + 1 < n and mat[i + 1][j + 1] != -1) {
  22.         res = max(res, rec(i + 1, j + 1) + mat[i][j]);
  23.     }
  24.     dp[i][j]  = res;
  25.     return res;
  26. }
  27. int main() {
  28.     cin >> n;
  29.    
  30.     memset(mat, -1, sizeof mat);
  31.     memset(dp, -1, sizeof dp);
  32.     for(int i = 0; i < n; i++) {
  33.         for(int j = 0; j <= i; j++) {
  34.             cin >> mat[i][j];
  35.         }
  36.     }
  37.    
  38.     cout << rec(0, 0) << endl;
  39.    
  40.    
  41.    
  42.    
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement