#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; constexpr int MAXV = 10000; constexpr int MAXIMUM = 20000; bool check[MAXIMUM + 1]; bool prevcheck[MAXIMUM + 1]; struct House { int v; int t; bool operator<(const House& h) const { return v + t < (h.v + h.t); } }; int main() { int N; cin >> N; vector house(N); for (ll i = 0; i < N; i++) { cin >> house[i].v >> house[i].t; } prevcheck[0] = true; sort(house.begin(), house.end()); for (ll i = 0; i < N; i++) { for (ll j = 0; j <= min(house[i].t - 1, MAXIMUM - house[i].v); j++) { if (prevcheck[j]) { check[j + house[i].v] = true; } } for (ll j = 0; j <= MAXIMUM; j++) { if (not prevcheck[j]) { prevcheck[j] = check[j]; } check[j] = false; } } for (auto i = MAXIMUM; i >= 0; i--) { if (prevcheck[i]) { cout << i << endl; break; } } return 0; }