Files
python/TangDou/AcWing/P1115_DP.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

19 lines
643 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const int INF = 0x3f3f3f3f;
int n, a[N], f[N], res = -INF;
// 定义f[i]为从1开始到i结束的所有数字中的最大子段和
// 从边界开始考虑如果f[1]=a[1];
// f[2] =max(a[2],a[2]+f[1])
// 推广通用公式: f[i]=max(f[i-1]+a[i],a[i])
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i]; //输入
f[i] = max(f[i - 1] + a[i], a[i]); // DP
res = max(res, f[i]); //取最大值也同时进行,节约时间
}
cout << res << endl;
return 0;
}