Positive earnings for the first swap deal: Policy is one by one sweep, sweep n times encountered can buy to buy, then you can get an update after r
The rest is seen as a backpack model: articles (a, b) represents the volume when the backpack> can be loaded at a time b, the volume of the article, the most asked several means
Disorderly enumerate all the items apparently wrong, to choose what to buy better
For two articles (ai, bi), (aj, bj), there are two sequential
Preemptive i, you need at least max (ai, aj + bi) volume
Preemptive j, then at least max (aj, ai + bj) volume
Because each buy an item, the remaining volume must be reduced in order to achieve a certain state, we have to choose the kind of volume requires a small order
And the comparator aj + bi ai + bj can be converted to compare aj-bj and ai-bi, this difference descending sort
/ * Sort backpack, to consider this question two articles (a1, b1), (a2 , b2) who should buy to buy a first minimum of max (a1, a2-b1) , a second member to buy a minimum of max ( A2, A1-B2) * / #include <bits / STDC ++ H.> the using namespace STD; #define N 30005 int DP [ 105 ] [N << . 1 ], n-, R & lt; struct the Node { int A, B; } C [ 105 ], D [ 105 ]; int CMP (the Node A, the Node B) { return A.A + ab &> B.A. + B.B.;} int CNT1, CNT2, VIS [N]; int main () { CIN > > >> n- R & lt; for ( int i=1;i<=n;i++){ int a,b;cin>>a>>b; if(b>0){ cnt1++; c[cnt1].a=a,c[cnt1].b=b; } else { cnt2++; d[cnt2].a=a,d[cnt2].b=b; } } int tot=0; for(int i=1;i<=cnt1;i++) for(int j=1;j<=cnt1;j++) if(!vis[j] && r>=c[j].a){ r+=c[j].b; vis[j]=1; tot++; break; } sort(d+1,d+1+cnt2,cmp); memset(dp,-0x3f,sizeof dp); dp[0][r]=tot; for(int i=1;i<=cnt2;i++) for(int j=0;j<=r;j++){ if(dp[i-1][j]>=0)//不选第i件 dp[i][j]=dp[i-1][j]; if(j-d[i].b>=d[i].a && dp[i-1][j-d[i].b]>=0)//选第i件 dp[i][j]=max(dp[i][j],dp[i-1][j-d[i].b]+1); } int ans=0; for(int j=r;j>=0;j--) if(dp[cnt2][j]>=0 ) ans = max (years dp [cnt2] [j]); cout << age << endl; }