#include #define show(x) cout << #x << " = " << x << endl using namespace std; using ll = long long; using pii = pair; using vi = vector; template ostream& operator<<(ostream& os, const vector& v) { os << "sz=" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template ostream& operator<<(ostream& os, const pair& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = 1e9 + 7; template constexpr T INF = numeric_limits::max() / 100; int main() { int n; cin >> n; vector A(n); vector B(n); for (ll i = 0; i < n; i++) { cin >> A[i] >> B[i]; } const int former_half = n / 2; const int latter_half = n - former_half; vector former; vector latter; const ll former_maximum = 1 << former_half; const ll latter_maximum = 1 << latter_half; for (ll i = 0; i < former_maximum; i++) { ll num = i; ll a = 0; ll b = 0; for (int j = 0; j < former_half; j++) { if (num % 2 == 1) { a += A[j]; } else { b += B[j]; } num /= 2; } former.push_back(a - b); } for (ll i = 0; i < latter_maximum; i++) { ll num = i; ll a = 0; ll b = 0; for (int j = 0; j < latter_half; j++) { if (num % 2 == 1) { a += A[j + former_half]; } else { b += B[j + former_half]; } num /= 2; } latter.push_back(a - b); } sort(latter.begin(), latter.end()); ll mini = INF; for (ll i = 0; i < former_maximum; i++) { const ll need = -former[i]; auto ite = lower_bound(latter.begin(), latter.end(), need); if (ite == latter.end()) { if (ite != latter.begin()) { mini = min(mini, abs(former[i] + (*(ite - 1)))); } } else { mini = min(mini, abs(former[i] + (*ite))); if (ite != latter.begin()) { mini = min(mini, abs(former[i] + (*(ite - 1)))); } } } cout << mini << endl; return 0; }