//#pragma GCC target("avx2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") #include using namespace std; using ll = long long; using pii = pair; using pll = pair; using pli = pair; #define TEST cerr << "TEST" << endl #define AMARI 998244353 //#define AMARI 1000000007 #define el '\n' #define El '\n' #define MULTI_TEST_CASE false void solve(void){ //問題を見たらまず「この問題設定から言えること」をいっぱい言う //一個回答に繋がりそうな解法が見えても、実装や細かい詰めに時間がかかりそうなら別の方針を考えてみる //添え字回りで面倒になりそうなときは楽になる言い換えを実装の前にじっくり考える //ある程度考察しても全然取っ掛かりが見えないときは実験をしてみる //よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く int n; cin >> n; vector a(n); int sum = 0; for(int i = 0; i < n; i++){ cin >> a[i]; sum += a[i]; } sort(a.begin(),a.end()); if(n < 20){ map mp; for(int S = 0; S < (1 << n); S++){ int temp = 0; for(int i = 0; i < n; i++){ if((S & (1 << i)))temp += a[i]; } mp[temp]++; } for(map::iterator it = mp.begin(); it != mp.end(); it++){ if(it->second >= 2 && sum - it->first * 2 != 0){ cout << sum - it->first * 2 << el; return; } } cout << -1 << el; return; } map mp; for(int S = 0; S < (1 << 16); S++){ int temp = 0; for(int i = 0; i < 16; i++){ if((S & (1 << i)))temp += a[i]; } mp[temp]++; } int m = -1; for(map::iterator it = mp.begin(); it != mp.end(); it++){ if(it->second >= 2){ m = it->first; break; } } assert(m != -1); vector> dp(n + 1,vector(m + 1,0)); dp[0][0] = 1; for(int i = 0; i < n; i++){ for(int j = 0; j <= m; j++){ dp[i + 1][j]++; if(j + a[i] <= m)dp[i + 1][j + a[i]]++; } } for(int i = 0; i <= m; i++){ if(dp[n][i] >= 2){ cout << sum - 2 * i << el; return; } } assert(0); return; } void calc(void){ return; } signed main(void){ cin.tie(nullptr); ios::sync_with_stdio(false); calc(); int t = 1; if(MULTI_TEST_CASE)cin >> t; while(t--){ solve(); } return 0; }