Files
python/GESP/Level6/202503T2_Prepare.cpp

30 lines
673 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 4e5 + 10;
int n;
LL a[N], s[N];
LL ans = -1e18;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[n + i] = a[i];
}
for (int i = 1; i <= 2 * n; i++)
s[i] = s[i - 1] + a[i];
// 对于每个位置i,枚举它前面n个位置,找最小前缀和
for (int i = 1; i <= 2 * n; i++) {
LL minPrefix = s[i - 1]; // 初始化为i-1位置的前缀和
for (int j = max(0, i - n); j <= i - 2; j++)
minPrefix = min(minPrefix, s[j]);
ans = max(ans, s[i] - minPrefix);
}
printf("%lld\n", ans);
return 0;
}