#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long int #define pii pair std::vector a; std::vector b; int main() { int n; cin >> n; a.resize(n); b.resize(n); for(int i = 0;i < n;i++) { cin >> a[i] >> b[i]; } std::unordered_set f; f.insert(0); for(int i = 0;i < n/2 ;i++) { auto t = a[i] + b[i]; std::vector v; for(auto e: f) { v.push_back(e + t); } for(auto e : v) { f.insert(e); } f.insert(t); } std::unordered_set s; s.insert(0); for(int i = n/2;i < n ;i++) { auto t = a[i] + b[i]; std::vector v; for(auto e: s) { v.push_back(e + t); } for(auto e : v) { s.insert(e); } s.insert(t); } std::vector vs; ll sum = 0; for(int i = 0;i < n;i++) { sum += a[i]; } ll ans = abs(sum); for(auto tt : s) { vs.push_back(tt); ans = min(ans, abs(sum - tt)); } std::sort(vs.begin(), vs.end()); for(auto tt : f) { auto l = std::lower_bound(vs.begin(), vs.end(), sum - tt); auto index = l - vs.begin(); for(int i = max(0, (int)index-1); i < vs.size(); i++) { auto c = sum - tt - vs[i]; ans = min(ans, abs(c)); if(c < 0) { break; } } } cout << ans << endl; }