#include #include #include #include #include #include #include #define REP(i, n) for(long long i = 0; i < n; ++i) #define ALL(a) a.begin(), a.end() #define INF (long long)1000000000 using namespace std; typedef long long ll; typedef pair P; int main() { int n; cin>>n; vector

a(n); REP(i, n) cin>>a[i].first>>a[i].second; int half = n/2; set aover1; set bover1; set aover2; set bover2; for(ll bit = 0; bit < (1<>i)&1) asum += a[i].first; else bsum += a[i].second; } if(asum >= bsum) aover1.insert(asum - bsum); if(asum <= bsum) bover1.insert(bsum - asum); } for(ll bit = 0; bit < (1<<(n - half)); ++bit) { ll asum = 0, bsum = 0; REP(i, n - half) { if((bit>>i)&1) asum += a[half + i].first; else bsum += a[half + i].second; } if(asum >= bsum) aover2.insert(asum - bsum); if(asum <= bsum) bover2.insert(bsum - asum); } ll res = INF * INF; if(aover1.size() > 0 && aover2.size() > 0) res = min(res, *(aover1.begin()) + *(aover2.begin())); if(bover1.size() > 0 && bover2.size() > 0) res = min(res, *(bover1.begin()) + *(bover2.begin())); auto ite1 = aover1.begin(); while(ite1 != aover1.end()) { auto ite2 = lower_bound(ALL(bover2), *ite1); if(ite2 != bover2.end()) { res = min(res, abs(*ite1 - *ite2)); } if(ite2 != bover2.begin()) { res = min(res, abs(*ite1 - *(--ite2))); } ++ite1; } auto ite3 = bover1.begin(); while(ite3 != bover1.end()) { auto ite4 = lower_bound(ALL(aover2), *ite3); if(ite4 != aover2.end()) { res = min(res, abs(*ite3 - *ite4)); } if(ite4 != aover2.begin()) { res = min(res, abs(*ite3 - *(--ite4))); } ++ite3; } cout<