模板题,降维时逆序,数据范围可能为负,第一次输入特判。
#include<iostream>
#include<algorithm>
using namespace std;
int n, m = -1e9, maxrode[501], nownum[501];
int main()
{
cin >> n;
fill(maxrode, maxrode + 501, -1e9);
for(int i = 1; i <= n; i++){
if(i == 1) {
scanf("%d", &nownum[1]);
continue;
}
for(int j = 1; j <= i; j++) scanf("%d", &nownum[j]);
for(int j = i; j >= 1; j--) maxrode[j] = max(maxrode[j], maxrode[j - 1]) + nownum[j];
}
for(int i = 1; i <= n; i++) m = max(m, maxrode[i]);
cout << m << endl;
}