/* -*- coding: utf-8 -*- * * 1583.cc: No.1583 Building Blocks - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 1000; const long long LINF = 1LL << 62; /* typedef */ typedef long long ll; typedef pair pii; /* global variables */ pii ps[MAX_N]; ll dp[MAX_N + 1]; /* subroutines */ inline void setmin(ll &a, ll b) { if (a > b) a = b; } /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int w, s; scanf("%d%d", &w, &s); ps[i] = pii(w + s, w); } sort(ps, ps + n); fill(dp, dp + n + 1, LINF); dp[0] = 0; for (int i = 0; i < n; i++) { int wi = ps[i].second, si = ps[i].first - wi; for (int j = i; j >= 0; j--) if (dp[j] <= si) setmin(dp[j + 1], dp[j] + wi); } int maxj = 0; for (int j = n; j >= 0; j--) if (dp[j] < LINF) { maxj = j; break; } printf("%d\n", maxj); return 0; }