#include #include #include #include #include #define INF (2 << 24) using namespace std; int n; int dp[10001]; bool chk[10001]; vector > vt; int rec(int sum){ if(dp[sum]) return dp[sum]; int ret = 0; for(int i = 0; i < n; i++){ if(sum < vt[i].second && chk[i]){ chk[i] = false; ret = max(ret, rec(sum + vt[i].first) + vt[i].first); chk[i] = true; } } return dp[sum] = ret; } int main(){ cin >> n; for(int i = 0; i < n; i++){ int v, t; cin >> v >> t; vt.push_back(make_pair(v, t)); } sort(vt.begin(), vt.end()); memset(chk, true, sizeof(chk)); cout << rec(0) << endl; }