#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; #define REP(i,n) for(int i=0; i<(n); ++i ) #define REPR(i,n) for(int i=(n); i>=0; --i) #define FOR(i,a,n) for(int i=(a); i<(n); ++i ) #define FORR(i,a,n) for(int i=(n); i>=(a); --i) #define DOUT(x) cerr << #x << " = " << (x) << "\n"; #define COUT(x) cout << (x) << "\n"; vector> a; vector b; int n; int f(int i, int sa) { if (i == n)return 0; if (b[i] != 0)return b[i]; int x = f(i + 1, sa - a[i].second) - a[i].second;; int y = f(i + 1, sa + a[i].first)+a[i].first; if (abs(x) < abs(y)) return b[i] = x; return b[i] = y; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; a.resize(n); b.resize(n, 0); REP(i, n) { cin >> a[i].first >> a[i].second; } COUT(f(0, 0)); return 0; }