题目。
感觉思路没错,但 64 分,求大佬纠正!
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int r;
bool f, f1;
int b[1000001];
int s[100001];
int c[100001];
int ans;
int xx, yy;
void inint() {
s[0] = 0;
f = f1 = 0;
for (int i = 1; i <= n; i++) {
cin >> c[i];
if (c[i] < 0) f = 1;
if (c[i] > 0) f1 = 1;
s[i] = s[i - 1] + c[i];
}
}
int fu() {
int ans = 0;
for (int i = 1; i <= n; i++) {
int t = s[n] - s[i] - c[i];
ans = max(t, ans);
}
return ans;
}
int zheng() {
int ans = 0;
for (int i = 1; i <= n ; i++) {
int t = -s[i] + s[n - 1] + c[i];
ans = max(t, ans);
}
return ans;
}
int zero() {
int ans = 0;
int t = 0;
xx = yy = 0;
for (int i = 1; i <= n; i++)
if (c[i] < 0) {
xx = i;
break;
}
xx--;
for (int i = n; i >= 1; i--)if (c[i] > 0) {
yy = i;
break;
}
yy++;
t = 0;
if (xx > 0)
for (int i = 1; i <= xx; i++) {
t = max(t, s[xx] - s[i] - c[i]);
}
ans += t;
// t = 0;
if (xx + 1 <= yy - 1)
for (int i = xx + 1; i <= yy - 1; i ++) {
ans += abs(c[i]);
}
//
t = 0;
if (yy <= n)
for (int i = yy + 1; i <= n; i ++) {
t = max(t, s[yy - 1] - s[i - 1] + c[i]);
}
ans += t;
return ans;
}
signed main() {
int t;
cin >> t;
while (t --) {
// f = f1 = 0;
// ans
cin >> n;
for (int i = 1; i <= n; i++) cin >> r;
// for (int i = 1; i <= n; i++) {
inint();
// if(n <= 8) {ans = solve();}
if (!f) {
ans = fu();
} else if (!f1) {
ans = zheng();
} else {
ans = zero();
}
// cout << ans << endl;
// }
// for(int i = 1; i <= n; i++) {
// if (f)
// ans = fu();
// if (f1) ans = zheng();
// else ans = zero();
//// cout << ans
// }
cout << ans << endl;
}
return 0;
}