#include using namespace std; #define endl '\n' #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end()) typedef long long ll; typedef pair P; typedef tuple T; typedef unsigned int uint; typedef unsigned long long ull; struct pairhash { public: template size_t operator()(const pair &x) const { size_t seed = hash()(x.first); return hash()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2); } }; const int inf = 1000000009; const double eps = 1e-8; vector vt; unordered_map memo; int calc(int sum, int num, int n) { int v = get<1>(vt[num]), t = get<2>(vt[num]); if (num == n-1) { return sum + (t > sum ? v : 0); } else { P p = make_pair(sum, num); if (memo.count(p) == 0) { int res = 0; if (t > sum) { res = calc(sum+v, num+1, n); } memo[p] = max(res, calc(sum, num+1, n)); } return memo[p]; } } int solve(int n) { for (int i = 0; i < n; i++) { int v, t; scanf("%d%d", &v, &t); vt.push_back(make_tuple(v+t, v, t)); } sort(all(vt)); return calc(0, 0, n); } int main() { /* ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); */ int n; scanf("%d", &n); printf("%d\n", solve(n)); }