#include <bits/stdc++.h> using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; using tll=tuple<ll,ll,ll>; 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<class T> inline bool chmin(T &a,T b){ if(a>b){ a=b; return true; } return false; } template<class T> inline bool chmax(T &a,T b){ if(a<b){ a=b; return true; } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; vector<ll> a(n); ll mx=0; rep(i,n){ cin >> a[i]; chmax(mx,a[i]); } mt19937 mt; shuffle(all(a),mt); if(n<=500) mx=2e4; else mx=1e4; vector<vector<ll>> dp(mx*2+1,vector<ll>(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<ll>(8,-INF)); } if(dp[mx][7]==-INF) cout << "-1\n"; else cout << dp[mx][7] << '\n'; }