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;
#define int long long
const int MOD1 = (int)1e9 + 7;
int n;
int A[505];
unsigned long long ways[505*505]; // natural overflow
int cnt[505*500];
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
int sum=0;
for(int i=1;i<=n;i++){
cin>>A[i];
sum+=A[i];
}
ways[0]=1;
ways[0]=1;
ways[0]=1;
for(int i=1;i<=n;i++){
for(int j=sum;j>=0;j--){
if(j-A[i]>=0)ways[j]=(ways[j]+ways[j-A[i]]);
}
}
unordered_set<int>os;
if(sum % 2 == 1){
return cout << 0,0;
}
if(ways[sum/2] == 0){
return cout << 0,0;
}
for(int i=1;i<=n;i++){ // remove this
unordered_set<int>os;
for(int j=0;j<=sum;j++){
if(j-A[i]>=0)ways[j]=(ways[j]-ways[j-A[i]]);
if(ways[j]){
int diff=abs(j-((sum-A[i])-j));
os.insert(diff);
}
}
for(auto x:os){
if(x>=0)cnt[x]++;
}
for(int j=sum;j>=0;j--){
if(j-A[i]>=0)ways[j]=(ways[j]+ways[j-A[i]]);
}
}
vector<int>out;
for(int j=1;j<=sum;j++){
if(cnt[j] == n){
out.push_back(j);
}
}
cout<<out.size()<<"\n";
for(auto x:out)cout<<x<<" ";
}
# | 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... |