poj 2184 Cow Exhibition 负值01背包

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=1<<24;

int dp[200000+5],s[100+5],f[10+5];

int main()
{
    int n,i,j,ans;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
            scanf("%d%d",&s[i],&f[i]);
        for(i=0;i<=200000;i++)
            dp[i]=-inf;
        dp[100000]=0;

        for(i=0;i<n;i++)
        {
            if(s[i]<=0&&f[i]<=0) continue;
            if(s[i]>0)
            {
                for(j=200000;j>=s[i];j--)

                    if(dp[j-s[i]]>-inf)
                        dp[j]=max(dp[j-s[i]]+f[i],dp[j]);
            }
            else
            {
                for(j=s[i];j<=200000+s[i];j++)
                {
                    if(dp[j-s[i]]>-inf)
                        dp[j]=max(dp[j-s[i]]+f[i],dp[j]);
                }
            }
        }

        ans=-inf;
        for(i=100000;i<=200000;i++)
        {
            if(dp[i]>=0)//要求两个都要大于0
                ans=max(ans,dp[i]+i-100000);
        }

        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(poj 2184 Cow Exhibition 负值01背包)