//In The Name Of ALLAH!
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define ld long double
#define endl '\n'
#define pb push_back
#define pf push_front
#define Zemur007 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define bpc __builtin_popcountll
#define btz __builtin_ctzll
#define all(x) x.begin() , x.end()
#define allr(x) x.rbegin() , x.rend()
#define F first
#define S second
#define pll pair<ll , ll>
#define turtle tuple<ll , ll , ll>
#define pss pair<string , string>
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define indexed_set tree<pll , null_type , less<pll> , rb_tree_tag , tree_order_statistics_node_update>
const ll sz = 5e2 + 5 , INF = 1e18 , MOD = 1e9 + 7;
ll a[sz];
unordered_map<ll , bool> dp;
map<ll , ll> pref[sz] , suff[sz];
ll mask , i , j , k;
void solve(){
ll n , SUM = 0 , con = 0; cin >> n;
for(i = 1 ; i <= n ; i++) cin >> a[i];
pref[0][0] = true;
for(i = 1 ; i <= n ; i++){
SUM += a[i];
for(j = 0 ; j <= SUM ; j++) pref[i][j] = pref[i - 1][j];
for(j = 0 ; j + a[i] <= SUM ; j++) pref[i][j + a[i]] |= pref[i - 1][j];
}
SUM = 0;
suff[n + 1][0] = true;
for(i = n ; i >= 1 ; i--){
SUM += a[i];
for(j = 0 ; j <= SUM ; j++) suff[i][j] = suff[i + 1][j];
for(j = 0 ; j + a[i] <= SUM ; j++) suff[i][j + a[i]] |= suff[i + 1][j];
}
vector<ll> ans;
for(k = 1 ; k <= SUM ; k++){
bool ok = true;
for(i = 1 ; i <= n ; i++){
ll cur = SUM + k - a[i];
if(cur & 1){
ok = false;
break;
}
bool check = false;
for(j = 0 ; j <= cur / 2 ; j++){
if(pref[i - 1][j] && suff[i + 1][cur / 2 - j]){
check = true;
break;
}
}
if(!check){
ok = false;
break;
}
}
if(!ok || (SUM & 1)) continue;
bool check = false;
for(j = 0 ; j <= SUM / 2 ; j++){
if(pref[n][j] && suff[n + 1][SUM / 2 - j]){
check = true;
break;
}
}
if(check) ans.pb(k);
}
cout << ans.size() << endl;
for(ll i : ans) cout << i << ' ';
cout << endl;
}
signed main(){
Zemur007;
//open;
ll t = 1;
// cin >> t;
while(t--){
solve();
}
// for(ll testcase = 1 ; testcase <= t ; testcase++){
// cout << "Case " << testcase << ":" << endl;
// solve();
// }
}
| # | 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... |