简单题解(短一点吧)
2024-04-14 10:17:28
发布于:江苏
0阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int a[107][107];
int dp[107][107];
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cin >> a[i][j];
dp[i][j] = -inf;
}
}
dp[1][1] = a[1][1];
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a[i + 1][j]);
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[i + 1][j + 1]);
}
}
int ans = -inf;
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[n][i]);
}
cout << ans;
}
这里空空如也
有帮助,赞一个