Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- int n;
- int mat[101][101];
- int rec(int i, int j) {
- if(i == n - 1) {
- return mat[i][j];
- }
- int res = 0;
- if(i + 1 < n) {
- if(mat[i + 1][j] != -1) {
- res = max(res, rec(i + 1, j) + mat[i][j]);
- }
- if(j + 1 < n and mat[i + 1][j + 1] != -1) {
- res = max(res, rec(i + 1, j + 1) + mat[i][j]);
- }
- }
- return res;
- }
- int main() {
- cin >> n;
- memset(mat, -1, sizeof mat);
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement