//#pragma GCC optimize ("-O3","unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i, n) for(int i = 0;i < n;i++) #define REPR(i, n) for(int i = n;i >= 0;i--) #define FOR(i, m, n) for(int i = m;i < n;i++) #define FORR(i, m, n) for(int i = m;i >= n;i--) #define SORT(v, n) sort(v, v+n); #define VSORT(v) sort(v.begin(), v.end()); #define REVERSE(v,n) reverse(v,v+n); #define VREVERSE(v) reverse(v.begin(), v.end()); #define ll long long #define pb(a) push_back(a) #define m0(x) memset(x,0,sizeof(x)) #define print(x) cout<P; const int MAX = 200020; int A[40], B[40]; int N; vectorv1, v2,ans; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; REP(i, N) { cin >> A[i] >> B[i]; } int h = N / 2; if (h == 0)goto through; REP(bit, 1LL << h) { int res = 0; REP(i, h) { if (bit & (1LL < i)) { res += A[i]; } else res -= B[i]; } v1.pb(res); //print(res); //print("here"); ans.pb(abs(res)); } through:; REP(bit, 1LL << (N - h)) { int res = 0; REP(i, N - h) { if (bit & (1 << i)) { res += A[h + i]; } else res -= B[h + i]; } //print(res); v2.pb(res); ans.pb(abs(res)); } v2.pb(-INF); v2.pb(INF); VSORT(v2); VSORT(ans); if (N == 1) { print(ans[0]); return 0; } VSORT(v1); ll ret = INF; REP(i, v1.size()) { auto itr = lb(v2, -v1[i]); int res1 = abs(v1[i] + *itr); itr--; int res2 = abs(v1[i] + *itr); itr++, itr++; int res3 = abs(v1[i] + *itr); ret = min({ ret,res1,res2,res3 }); } print(ret); }