// yukicoder: No.545 ママの大事な二人の子供 // 2019.4.25 bal4u #include #include #if 1 #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) #else #define gc() getchar() #define pc(c) putchar(c) #endif long long in() // 非負整数の入力 { long long n = 0; int c = gc(); do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0'); return n; } long long a[35], b[35]; int N; long long s[65600]; int sz; void precalc(long long *a, long long *b, long long *s, int n) { int i, j; long long _s = 0; sz = 1 << n; for (i = 0; i < sz; i++) { _s = 0; for (j = 0; j < n; j++) { if ((i >> j) & 1) _s += a[j]; else _s -= b[j]; } s[i] = _s; } } int cmp(const void *a, const void *b) { if (*(long long *)a < *(long long *)b) return -1; return *(long long *)a >= *(long long *)b; } int bsch(long long x) { int m, l = 0, r = sz; while (l+1 < r) { m = (l+r) >> 1; if (s[m] < x) l = m; else r = m; } return l; } #define INF 0x7ffffffffffffLL #define ABS(x) ((x)>=0?(x):-(x)) int main() { int i, k, N, n, lim; long long ans; N = (int)in(); for (i = 0; i < N; i++) a[i] = in(), b[i] = in(); if (N == 1) { ans = a[0]; if (b[0] < ans) ans = b[0]; printf("%lld\n", ans); return 0; } n = (N+1)/2; precalc(a, b, s, n); qsort(s, sz, sizeof(long long), cmp); s[sz] = INF, k = N - n, lim = 1 << k, ans = INF; for (i = 0; i < lim; i++) { long long t, d = 0; int m, j; for (j = 0; j < k; j++) { if ((i >> j) & 1) d += a[j+n]; else d -= b[j+n]; } m = bsch(-d); if ((t = ABS(s[m]+d)) < ans) ans = t; if ((t = ABS(s[m+1]+d)) < ans) ans = t; if (ans == 0) break; } printf("%lld\n", ans); return 0; }