//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 0xccccccc; const ll LINF = 922337203685477580LL; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} const int N = 1<<16; //head int n; ll U[N]; vector> u; ll ans = LINF; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; rep(i, n>>1) { ll a, b; cin >> a >> b; rep(j, 1<<(n>>1)) { if(j>>i&1) U[j] += a; else U[j] -= b; } } sort(U, U+(1<<(n>>1))); rep(i, (n+1)>>1) { ll a, b; cin >> a >> b; u.emplace_back(a, b); } rep(i, 1<<((n+1)>>1)) { ll now = 0; rep(j, (n+1)>>1) { if(i>>j&1) now += u[j].first; else now -= u[j].second; } ll *itr = upper_bound(U, U+(1<<(n>>1)), -now); if(itr != U+(1<<(n>>1))) chmin(ans, abs(*itr+now)); if(itr != U) chmin(ans, abs(now+*(itr-1))); } cout << ans << endl; }