CCCPaste

Untitled

#include 
#include 
using namespace std;

int n;
int mat[101][101];
int dp[101][101];
const int INF = 1e8;
int rec(int i, int j) {
    if(i == n - 1) {
        return mat[i][j];
    }
    if(dp[i][j] != -1) {
        return dp[i][j];
    }
    
    int res = -INF;
    if(i + 1 < n and mat[i + 1][j] != -1) {
        res = max(res, rec(i + 1, j) + mat[i][j]);
    }
    if(i + 1 < n and j + 1 < n and mat[i + 1][j + 1] != -1) {
        res = max(res, rec(i + 1, j + 1) + mat[i][j]);
    }
    dp[i][j]  = res;
    return res;
}
int main() {
    cin >> n;
    
    memset(mat, -1, sizeof mat);
    memset(dp, -1, sizeof dp);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= i; j++) {
            cin >> mat[i][j];
        }
    }
    
    cout << rec(0, 0) << endl;
    
    
    
    
    return 0;
}