标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9530 | Accepted: 3673 |
Description
Input
Output
Sample Input
5 -5 7 8 -6 6 -3 2 1 -8 -5
Sample Output
8
Hint
Source
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> using namespace std; const int N = 110; const int M = 200010; const int NN = 1000; const int INF = 10000000; int w[N],v[N],sum; int dp[M], cnt[M]; int main() { int n,ans; int i,j; while(~scanf("%d", &n)) { sum = 0; for(i = 1; i <= n; i++) { scanf("%d%d", v+i, w+i); if(w[i] <= 0 && v[i] <= 0) {i--,n--; continue;} v[i] += 1000; sum += v[i]; } for(i = 1; i <= sum; dp[i++] = -INF); dp[0] = 0; memset(cnt, 0, sizeof(cnt)); for(i = 1; i <= n; i++) for(int j = sum; j >= v[i]; j--) if(dp[j-v[i]]+w[i]-(cnt[j-v[i]]+1)*NN > dp[j]-cnt[j]*NN) { dp[j] = dp[j-v[i]]+w[i]; cnt[j] = cnt[j-v[i]]+1; } ans = 0; for(i = 0; i <= sum; i++) if(dp[i] >= 0 && i-cnt[i]*NN >= 0) ans = max(ans, dp[i]+i-cnt[i]*NN); printf("%d\n", ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4224013.html