#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; int check[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; } check[0] = 1; sort(house.begin(), house.end()); check[house[0].v] = 2; int maxi = house[0].v; for (ll i = 1; i < N; i++) { ll maxi2 = maxi; for (ll j = max(0, maxi - house[i].v + 1); j <= min(house[i].t - 1, MAXIMUM - house[i].v); j++) { if (check[j] > 0 and check[j] <= i + 1) { maxi2 = max(j + house[i].v, maxi2); check[j + house[i].v] = i + 2; } } maxi = maxi2; } for (auto i = MAXIMUM; i >= 0; i--) { if (check[i] > 0) { cout << i << endl; break; } } return 0; }