#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++) using namespace std; typedef long long int ll; typedef vector VI; typedef vector VL; typedef pair PI; int main(void) { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VL d(n); ll atot = 0; REP(i, 0, n) { ll a, b; cin >> a >> b; atot += a; d[i] = b + a; } sort(d.begin(), d.end()); set former; set latter; REP(bits, 0, 1 << (n / 2)) { ll sum = 0; REP(i, 0, n / 2) { if (bits & 1 << i) { sum += d[i]; } } former.insert(sum); } REP(bits, 0, 1 << (n - (n / 2))) { ll sum = 0; REP(i, 0, n - n / 2) { if (bits & 1 << i) { sum += d[i + n / 2]; } } latter.insert(sum); } VL fv(former.begin(), former.end()); VL lv(latter.begin(), latter.end()); sort(fv.begin(), fv.end()); sort(lv.begin(), lv.end()); ll mi = 1e15; REP(i, 0, fv.size()) { ll t = fv[i]; // We are to find position p s.t. abs(atot - t - lv[p]) is smallest. ll rem = atot % 2 == 0 ? 0 : 1; int lo = upper_bound(lv.begin(), lv.end(), (atot - rem) - t) - lv.begin(); int hi = lower_bound(lv.begin(), lv.end(), (atot + rem) - t) - lv.begin(); if (lo >= 1) { mi = min(mi, -(lv[lo - 1] + t) + atot); } if (hi < (int) lv.size()) { mi = min(mi, -atot + (lv[hi] + t)); } } cout << mi << "\n"; }