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;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int nx=505, kx=250005;
int n, a[nx], sm;
bitset<kx> dp[nx], l, r;
vector<int> res;
bool valid(int x)
{
for (int i=1; i<=n; i++) if (((sm-a[i]+x)%2)||(!dp[i][(sm-a[i]+x)/2])) return 0;
return 1;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>a[i], sm+=a[i];
for (int i=0; i<=n; i++) dp[i][0]=1;
l[0]=r[0]=1;
for (int i=1; i<=n; i++)
{
if (i>=n/2) dp[i]=l;
l=l|(l<<a[i]);
}
dp[0]=l;
for (int i=n; i>=1; i--)
{
if (i<n/2) dp[i]=r;
r=r|(r<<a[i]);
}
for (int i=1; i<=n; i++)
{
if (i>=n/2) for (int j=i+1; j<=n; j++) dp[i]=dp[i]|(dp[i]<<a[j]);
else for (int j=1; j<i; j++) dp[i]=dp[i]|(dp[i]<<a[j]);
}
if ((sm%2)||!dp[0][sm/2]) return cout<<0, 0;
for (int i=1; i<kx; i++) if (valid(i)) res.push_back(i);
cout<<res.size()<<'\n';
for (auto x:res) cout<<x<<' ';
}
/*
4
1 3 1 5
*/
# | 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... |