[Explanations] luogu p2340 cows Exhibition

Summary:
1. IQ where to start the cycle did not expect.

2. The separate discussion of positive and negative IQ. Negative IQ order to use, guaranteed not to make a multi-purpose cow.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN = 800005;
 4 int dp[MAXN], n, iq[405], eq[405], maxx = -2000000;
 5 int main()
 6 {
 7     memset(dp, -0x3f, sizeof(dp));
 8     dp[400000] = 0;
 9     cin >> n;
10     for(int i = 1; i <= n; i++)
11         cin >> iq[i] >> eq[i];
12     for(int i = 1; i <= n; i++)
13         if(iq[i] >= 0)
14         {
15             for(int j = 800000; j >= iq[i]; j--)
16                 dp[j] = max(dp[j], dp[j-iq[i]]+eq[i]);
17         }
18         else {
19             for(int j = 0; j <= 800000+iq[i]; j++)
20                 dp[j] = max(dp[j], dp[j-iq[i]]+eq[i]);
21         }
22     for(int i = 400000; i <= 800000; i++)
23         if(dp[i] >= 0) maxx = max(maxx, i-400000+dp[i]);
24      cout << maxx;
25     return 0;
26 }

Deformation backpack

Guess you like

Origin www.cnblogs.com/lovezxy520/p/11306393.html