Advertisement
Josif_tepe

Untitled

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