You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#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;
|
|
|
|
|
}
|