#include #include #include #include #include #include #include #define repeat(i,n) for (int i = 0; (i) < int(n); ++(i)) #define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) using namespace std; template void setmax(T & a, T const & b) { if (a < b) a = b; } template void setmin(T & a, T const & b) { if (b < a) a = b; } int main() { chrono::high_resolution_clock::time_point clock_begin = chrono::high_resolution_clock::now(); int n; cin >> n; vector v(n), t(n); repeat (i,n) cin >> v[i] >> t[i]; int ans_upper = 0; repeat (i,n) setmax(ans_upper, t[i]-1 + v[i]); setmin(ans_upper, whole(accumulate, v, 0)); int ans = 0; auto dp = make_unique >(); auto it = make_unique >(); vector ix(n); whole(iota, ix, 0); whole(iota, ix, 0); whole(sort, ix, [&](int i, int j) { return t[i] < t[j]; }); while (true) { chrono::high_resolution_clock::time_point clock_end = chrono::high_resolution_clock::now(); if (chrono::duration_cast(clock_end - clock_begin).count() >= 4900) break; dp->reset(); (*dp)[0] = true; for (int i : ix) { it->set(); *it <<= t[i]; it->flip(); *it &= *dp; *it <<= v[i]; *dp |= *it; } for (int i = ans_upper; i >= ans; -- i) { if ((*dp)[i]) setmax(ans, i); } whole(random_shuffle, ix); } cout << ans << endl; return 0; }