我用搜索进了最优解第一页:
#include<bits/stdc++.h>
using namespace std;
int n, iq, eq, len = 1, ans;
array<int, 405> w, v, a, b, c;
void dfs(int i, int x, int y) {
static int cnt = 0;
cnt++;
if (cnt > 100000000) {
cout << ans;
exit(0);
} else if (i == len + 1) {
if (x >= 0 && y >= 0) {
ans = max(ans, x + y);
}
} else if (x + y + a[i] > ans && x + b[i] >= 0 && y + c[i] >= 0) {
dfs(i + 1, x + w[i], y + v[i]);
dfs(i + 1, x, y);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i, ++len) {
int x, y;
cin >> x >> y;
if (x + y >= 0) {
if (x >= 0 && y >= 0) {
iq += x;
eq += y;
} else {
w[len] = x;
v[len] = y;
}
} else {
len--;
}
}
for (int i = len; i > 0; --i) {
a[i] = a[i + 1] + w[i] + v[i];
b[i] = b[i + 1] + max(0, w[i]);
c[i] = c[i + 1] + max(0, v[i]);
}
ans = iq + eq;
dfs(1, iq, eq);
cout << ans;
return 0;
}