AcWing 898. 数字三角形

//从上往下
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 510, INF = 1e9;
int n;
int a[N][N];
int f[N][N];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= i; j ++ )
            scanf("%d", &a[i][j]);
    for (int i = 0; i <= n; i ++ )
        //要初始化一个,比如这一行最后一个的右上没有,要初始化为负无穷
        for (int j = 0; j <= i + 1; j ++ )
            f[i][j] = -INF;
    f[1][1] = a[1][1];//
    for (int i = 2; i <= n; i ++ )
        for (int j = 1; j <= i; j ++ )
            f[i][j] = max(f[i - 1][j - 1] + a[i][j], f[i - 1][j] + a[i][j]);
    int res = -INF;
    for (int i = 1; i <= n; i ++ ) res = max(res, f[n][i]);//取最后一行的最大值
    printf("%d\n", res);
    return 0;
}

 

 

AcWing 898. 数字三角形

 

AcWing 898. 数字三角形

上一篇:AcWing 897. 最长公共子序列


下一篇:shell脚本移植问题(windwos移植到linux)