#include <iostream>
#include <stdio.h>
#include <unordered_set>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
const int MAXN=500, MAXSUM=500*500;
int a[MAXN+1];
bool dp[MAXN][MAXSUM+1];
void AllPossSums(int arr[], int n, vector<int> &);
int main()
{
int n, sum=0;
cin>>n;
set<int> mods;
for (int i=0; i<n; i++)
{
scanf("%d", a+i);
mods.insert(a[i]%2);
if (a[i]%2==0)
a[i]/=2;
sum+=a[i];
}
if (mods.size()>1)
{
cout<<"0\n";
return 0;
}
if (*(mods.begin()))
if (n%2)
{
cout<<"0\n";
return 0;
}
vector<int> curSums;
AllPossSums(a, n, curSums);
bool f=false;
for (int i=0; i<curSums.size(); i++)
if (curSums[i]*2==sum)
{
f=true;
break;
}
if (!f)
{
cout<<"0\n";
return 0;
}
unordered_set<int> ans, curAns;
for (int i=0; i<n; i++)
{ // #i doesn't play.
sum-=a[i];
swap(a[i], a[n-1]);
AllPossSums(a, n-1, curSums);
curAns.clear();
for (int j=0; j<curSums.size(); j++)
curAns.insert(abs(sum-curSums[j]-curSums[j]));
if (!i)
{
ans=curAns;
swap(a[i], a[n-1]);
sum+=a[i];
continue;
}
unordered_set<int> temp;
if (curAns.size()<ans.size())
{
for (auto it=curAns.begin(); it!=curAns.end(); it++)
if (ans.find(*it)!=ans.end())
temp.insert(*it);
}
else
for (auto it=ans.begin(); it!=ans.end(); it++)
if (curAns.find(*it)!=curAns.end())
temp.insert(*it);
ans=temp;
swap(a[i], a[n-1]);
sum+=a[i];
}
cout<<ans.size()<<'\n';
set<int> ansOut(ans.begin(), ans.end());
for (auto it=ansOut.begin(); it!=ansOut.end(); it++)
cout<<*it<<' ';
cout<<endl;
char I;
cin >> I;
return 0;
}
void AllPossSums(int arr[], int n, vector<int> & res)
{
int sum=0;
for (int i=0; i<n; i++)
sum+=arr[i];
memset(dp, 0, sizeof(dp));
for (int i=0; i<=n; i++)
dp[i][0]=true;
for (int i=1; i<=n; i++)
{
dp[i][arr[i-1]]=true;
for (int j=1; j<=sum; j++)
{
if (dp[i-1][j]==true)
{
dp[i][j]=true;
dp[i][j+arr[i-1]]=true;
}
}
}
res.clear();
for (int j=0; j<=sum; j++)
if (dp[n][j]==true)
res.push_back(j);
}
Compilation message
bootfall.cpp: In function 'int main()':
bootfall.cpp:42:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<curSums.size(); i++)
~^~~~~~~~~~~~~~~
bootfall.cpp:60:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0; j<curSums.size(); j++)
~^~~~~~~~~~~~~~~
bootfall.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", a+i);
~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
122744 KB |
Output is correct |
2 |
Correct |
222 ms |
122744 KB |
Output is correct |
3 |
Correct |
83 ms |
122800 KB |
Output is correct |
4 |
Incorrect |
174 ms |
122940 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
122744 KB |
Output is correct |
2 |
Correct |
222 ms |
122744 KB |
Output is correct |
3 |
Correct |
83 ms |
122800 KB |
Output is correct |
4 |
Incorrect |
174 ms |
122940 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
122744 KB |
Output is correct |
2 |
Correct |
222 ms |
122744 KB |
Output is correct |
3 |
Correct |
83 ms |
122800 KB |
Output is correct |
4 |
Incorrect |
174 ms |
122940 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
122744 KB |
Output is correct |
2 |
Correct |
222 ms |
122744 KB |
Output is correct |
3 |
Correct |
83 ms |
122800 KB |
Output is correct |
4 |
Incorrect |
174 ms |
122940 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
122744 KB |
Output is correct |
2 |
Correct |
222 ms |
122744 KB |
Output is correct |
3 |
Correct |
83 ms |
122800 KB |
Output is correct |
4 |
Incorrect |
174 ms |
122940 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
122744 KB |
Output is correct |
2 |
Correct |
222 ms |
122744 KB |
Output is correct |
3 |
Correct |
83 ms |
122800 KB |
Output is correct |
4 |
Incorrect |
174 ms |
122940 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |