This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
const int MAX = N * N;
int n, a[N];
int sum;
int dp[MAX]; // knapsack
int tmp[MAX]; // for solve(pos)
int check[MAX];
void solve(int pos) {
for (int i = 0; i <= sum; ++i) tmp[i] = dp[i];
for (int i = 0; i <= sum; ++i) {
// we do not use a[pos]
if (i >= a[pos]) {
tmp[i] -= tmp[i - a[pos]];
}
}
for (int i = 0; i <= sum; ++i) {
if (tmp[i] != 0 && a[pos] + 2 * i - sum >= 0)
check[a[pos] + 2 * i - sum]++;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
// prepare KNAPSACK
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
sum += a[i];
for (int j = sum; j >= a[i]; --j) {
dp[j] += dp[j - a[i]];
}
}
if ((sum & 1) || (dp[sum / 2] == 0)) return printf("0\n"), 0; // invalid
for (int i = 1; i <= n; ++i) solve(i);
int cnt = 0;
for (int i = 1; i <= sum; ++i) if (check[i] == n) ++cnt;
printf("%d\n", cnt);
for (int i = 1; i <= sum; ++i) if (check[i] == n) printf("%d ", i); printf("\n");
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |