#include using namespace std; using ll = long long; int main(){ int N; cin >> N; vector A(N); for (ll& x : A) cin >> x; int M = N/2; vector L(A.begin(), A.begin()+M); vector R(A.begin()+M, A.end()); vector> LS(M+1), RS(M+1); for (int bit = 0; bit < (1<>i&1) ls += L[i], rs += R[i], pc++;; } LS[pc].push_back(ls); RS[pc].push_back(rs); } for (auto& a : LS) sort(a.begin(), a.end()); for (auto& a : RS) sort(a.begin(), a.end()); ll S = reduce(A.begin(), A.end()); ll ans = LLONG_MAX; for (int i = 0; i <= M; i++){ for (ll x : LS[i]){ ll tar = S/2-x; auto it = lower_bound(RS[M-i].begin(), RS[M-i].end(), tar); if (it != RS[M-i].end()) ans = min(ans, abs(S-2*(x+*it))); if (it != RS[M-i].begin()) ans = min(ans, abs(S-2*(x+*(--it)))); } } cout << ans << endl; }