/* -*- coding: utf-8 -*- * * 545.cc: No.545 ママの大事な二人の子供 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 32; const int MAX_H = MAX_N / 2; const int HBITS = 1 << MAX_H; typedef long long ll; const ll LINF = 1LL << 60; /* typedef */ typedef vector vi; typedef queue qi; typedef pair pii; /* global variables */ ll as[MAX_N], bs[MAX_N]; ll vp[HBITS], vq[HBITS]; /* subroutines */ void setv(int i0, int i1, ll v[]) { int k = i1 - i0; int kbits = 1 << k; for (int bits = 0; bits < kbits; bits++) { ll sum = 0; for (int i = i0, bi = 1; i < i1; i++, bi <<= 1) { if (bits & bi) sum += bs[i]; else sum -= as[i]; } v[bits] = sum; } } void printv(int n, ll v[]) { for (int i = 0; i < n; i++) printf("%lld ", v[i]); putchar('\n'); } /* main */ int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> as[i] >> bs[i]; if (n == 1) { printf("%lld\n", min(as[0], bs[0])); return 0; } int pn = n / 2, qn = n - pn; int pbits = 1 << pn, qbits = 1 << qn; setv(0, pn, vp); setv(pn, n, vq); sort(vp, vp + pbits); sort(vq, vq + qbits); //printv(pbits, vp), printv(qbits, vq); ll mind = LINF; for (int bits = 0; bits < pbits; bits++) { ll &p = vp[bits]; int qi = lower_bound(vq, vq + qbits, -p) - vq; if (qi > 0) { ll d0 = abs(p + vq[qi - 1]); //printf("p=%lld, qi=%d, d0=%lld\n", p, qi, d0); if (mind > d0) mind = d0; } if (qi < qbits) { ll d1 = abs(p + vq[qi]); //printf("p=%lld, qi=%d, d1=%lld\n", p, qi, d1); if (mind > d1) mind = d1; } } printf("%lld\n", mind); return 0; }