首页 > 代码库 > hdu 1171 Big Event in HDU(01背包)

hdu 1171 Big Event in HDU(01背包)

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int a[5005];
int dp[130005];

int main()
{
    int n;
    while(scanf("%d",&n)&&n>=0)
    {
        int k=1;
        int s=0;
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            while(y--)
            {
                a[k]=x;
                s+=x;
                k++;
            }
        }
        int ss=s;
        s=s/2;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<k;i++)
        {
            for(int v=s;v-a[i]>=0;v--)
            {
                dp[v]=max(dp[v],dp[v-a[i]]+a[i]);
            }
        }
        printf("%d %d\n",ss-dp[s],dp[s]);
    }
    return 0;
}


hdu 1171 Big Event in HDU(01背包)