#include #include #include using namespace std; const long long INF = 1000000000000; int main(){ int n; cin >> n; vector A(n), B(n); for (int i = 0; i < n; i++){ cin >> A[i] >> B[i]; } int x = n / 2; int y = n - x; vector p(1 << x, 0); for (int i = 0; i < (1 << x); i++){ for (int j = 0; j < x; j++){ if (i >> j){ p[i] += A[j]; } else { p[i] -= B[j]; } } } vector q(1 << y, 0); for (int i = 0; i < (1 << y); i++){ for (int j = 0; j < y; j++){ if (i >> j){ q[i] += A[x + j]; } else { q[i] -= B[x + j]; } } } sort(p.begin(), p.end()); sort(q.begin(), q.end()); long long ans = INF; for (int i = 0; i < (1 << x); i++){ auto itr = lower_bound(q.begin(), q.end(), -p[i]); if (itr != q.end()){ ans = min(ans, abs(p[i] + *itr)); } if (itr != q.begin()){ ans = min(ans, abs(p[i] + *prev(itr))); } } cout << ans << endl; return 0; }