总结:
1.智商从哪开始循环没想到。2.将智商的正负分开讨论。负智商要用顺序,保证不会使一头牛多用。
1 #include2 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 }
背包的变形