1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <set> #include <vector> #include <climits> using namespace std;
const int INF = ~0u>>1; typedef pair <int,int> P; #define MID(x,y) ((x+y)>>1) #define iabs(x) ((x)>0?(x):-(x)) #define REP(i,a,b) for(int i=(a);i<(b);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define pb push_back #define mp make_pair #define print() cout<<"--------"<<endl const int maxn = 100005; int dp[maxn * 2]; int n; struct node{ int s,f; }p[200]; int main(){ while (~scanf("%d", &n)){ for (int i = 0; i < 2 * maxn; i++) dp[i] = INT_MIN; for (int i = 0; i < n; i++) scanf("%d%d", &p[i].s, &p[i].f); dp[100000] = 0; for (int i = 0; i < n; i++){ if(p[i].s >= 0){ for (int j = 2 * maxn - 1; j >= p[i].s; j--){ if (dp[j-p[i].s] > INT_MIN) dp[j] = max(dp[j],dp[j-p[i].s]+p[i].f); } }else { for (int j = 0; j - p[i].s <= 200000; j++){ if (dp[j-p[i].s] > INT_MIN) dp[j] = max(dp[j],dp[j-p[i].s]+p[i].f); } } } int ans = 0; for (int i = 100000; i < 2 * maxn; i++) if(dp[i] >= 0) ans = max(ans,dp[i]+i-100000); printf("%dn", ans); } return 0; }
|