标签:
1 #include<cstdio> 2 #include<cstring> 3 4 const int maxn=200000+10; 5 const int inf=0x3f3f3f3f; 6 7 int dp[maxn]; 8 int s[105]; 9 int f[105]; 10 11 int max(int a,int b) 12 { 13 return a>b?a:b; 14 } 15 16 int main() 17 { 18 int n; 19 while(~scanf("%d",&n)) 20 { 21 int up=100000,down=100000; 22 for(int i=1;i<=n;i++) 23 { 24 scanf("%d%d",&s[i],&f[i]); 25 if(s[i]>0) 26 up+=s[i]; 27 else 28 down+=s[i]; 29 } 30 31 for(int i=down;i<=up;i++) 32 dp[i]=-inf; 33 dp[100000]=0; 34 for(int i=1;i<=n;i++) 35 { 36 if(s[i]<=0&&f[i]<=0) 37 continue; 38 if(s[i]>0) 39 { 40 for(int j=up;j>=down+s[i];j--) 41 { 42 dp[j]=max(dp[j],dp[j-s[i]]+f[i]); 43 } 44 } 45 else 46 { 47 for(int j=down;j<=up+s[i];j++) 48 { 49 dp[j]=max(dp[j],dp[j-s[i]]+f[i]); 50 } 51 } 52 } 53 int ans=0; 54 for(int i=100000;i<=up;i++) 55 { 56 if(dp[i]>=0) 57 ans=max(ans,i+dp[i]-100000); 58 } 59 printf("%d\n",ans); 60 } 61 return 0; 62 }
POJ 2184 Cow Exhibition 01背包变型 好题
标签:
原文地址:http://www.cnblogs.com/-maybe/p/4692556.html