#include using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair; using tll=tuple; using ld=long double; const ll INF=(1ll<<60); #define rep(i,n) for (ll i=0;i<(ll)(n);i++) #define replr(i,l,r) for (ll i=(ll)(l);i<(ll)(r);i++) #define all(v) v.begin(),v.end() #define len(v) ((ll)v.size()) template inline bool chmin(T &a,T b){ if(a>b){ a=b; return true; } return false; } template inline bool chmax(T &a,T b){ if(a> n; vector a(n); ll mx=0; rep(i,n){ cin >> a[i]; chmax(mx,a[i]); } mt19937 mt; shuffle(all(a),mt); mx=max(1e7/n,10000); vector> dp(mx*2+1,vector(8,-INF)),ndp=dp; dp[mx][0]=0; rep(i,n){ rep(j,mx*2+1){ rep(k,8){ if(dp[j][k]==-INF) continue; chmax(ndp[j][k|1],dp[j][k]+a[i]); if(j+a[i]<=mx*2) chmax(ndp[j+a[i]][k|2],dp[j][k]); if(0<=j-a[i]) chmax(ndp[j-a[i]][k|4],dp[j][k]); } } swap(dp,ndp); ndp.assign(mx*2+1,vector(8,-INF)); } if(dp[mx][7]==-INF) cout << "-1\n"; else cout << dp[mx][7] << '\n'; }