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>
#define DIM 502
using namespace std;
bool dp_left[DIM][DIM*DIM],dp_right[DIM][DIM*DIM],dp[DIM];
int v[DIM],f[DIM*DIM];
vector <int> sol,w,w2;
int n,i,j,sum_total;
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n;
for (i=1;i<=n;i++){
cin>>v[i];
sum_total += v[i];
}
if (sum_total % 2){
cout<<"0";
return 0;
}
dp[0] = 1;
for (i=1;i<=n;i++){
for (j=sum_total-v[i];j>=0;j--){
if (dp[j])
dp[j+v[i]] = 1;
}
}
if (!dp[sum_total/2]){
cout<<"0";
return 0;
}
dp_left[0][0] = 1;
for (i=1;i<=n;i++){
memcpy (dp_left[i],dp_left[i-1],sizeof dp_left[i-1]);
for (j=sum_total-v[i];j>=0;j--){
if (dp_left[i][j])
dp_left[i][j+v[i]] = 1;
}
}
dp_right[n+1][0] = 1;
for (i=n;i;i--){
memcpy (dp_right[i],dp_right[i+1],sizeof dp_right[i+1]);
for (j=sum_total-v[i];j>=0;j--){
if (dp_right[i][j])
dp_right[i][j+v[i]] = 1;
}
}
for (i=1;i<=n;i++){
w.clear(); w2.clear();
for (int j=0;j<=sum_total;j++){
if (dp_left[i-1][j])
w.push_back(j);
if (dp_right[i+1][j])
w2.push_back(j);
}
int sum = sum_total - v[i];
for (j=1;j<=sum_total;j++){
if ( (sum + j)%2 )
continue;
int val = (sum + j)/2; /// exista suma asta?
if (dp_left[i-1][val] || dp_right[i+1][val]){
f[j]++;
continue;
}
int ok = 0;
for (auto it : w){
if (it > val)
break;
if (dp_right[i+1][val-it-j]){
f[j]++;
ok = 1;
break;
}
}
if (ok)
continue;
for (auto it : w2){
if (it > val)
break;
if (dp_left[i-1][val-it-j]){
f[j]++;
break;
}}}}
for (i=1;i<=sum_total;i++){
if (f[i] == n)
sol.push_back(i);
}
cout<<sol.size()<<"\n";
for (auto it : sol)
cout<<it<<" ";
return 0;
}
# | 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... |