题解:P10811 【MX-S2-T2】排
silhouettel · · 题解
观察一下性质,发现若是序列全为正数或全为负数或全为零,排序输出最大值即可。
接下来考虑序列中既有正数也有负数的情况。可以发现,此时序列中的零是无意义的,不论你放哪都不能造成影响,所以我们把它去掉,原因后面说。又根据题面中提供的式子,当前位为正的话,不能再叠加正数了,所以答案不会超过序列中的最大值。这时候就会考虑,除去最大值后的
解释一下去零的问题,由于至少一个非零数会被选,所以那
bitset 只要开
#include <bits/stdc++.h>
using namespace std;
#define real signed
#define enl '\n'
const int iinf = 0x3f3f3f3f;
const int st = 2000001;
int n;
int a[2011];
vector<int> se;
bitset<4000011> f;
real main() {
std::ios::sync_with_stdio(false), std::cin.tie(NULL), std::cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++)
if (a[i]) se.push_back(a[i]);
sort(se.begin(), se.end());
bool flg = true;
for (int i = 1; i <= n; i++)
if (a[i] * a[1] < 0) flg = false;
if (se.empty()) cout << 0;
else if (flg) cout << se[se.size() - 1];
else {
for (int i = 0; i < se.size() - 1; i++) {
if (se[i] > 0) f |= f << se[i];
else f |= f >> -se[i];
f[st + se[i]] = 1;
}
int ma = -iinf;
for (int i = st - 2000; i <= st; i++)
if (f[i]) ma = i;
cout << ma - st + se[se.size() - 1];
}
return 0;
}