B - Big Event in HDU
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
A test case starting with a negative integer terminates input and this test case is not to be processed.
2 10 1 20 1 3 10 1 20 2 30 1 -1
20 10 40 40
转载来自http://blog.csdn.net/hellobabygogo3/article/details/8001581
c++代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int dp[250025];
int v[100],num[100];
int main()
{
int i,j,n,sum,cnt,k;
while(scanf("%d",&n),n>0)
{
cnt=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&v[i],&num[i]);
cnt+=v[i]*num[i];
}//先求出总的和
memset(dp,0,sizeof(dp));
sum=cnt/2;
for(i=1;i<=n;i++)
for(j=1;j<=num[i];j++)
for(k=sum;k>=v[i];k-=v[i])
dp[k]=max(dp[k],dp[k-v[i]]+v[i]);
printf("%d %d\n",cnt-dp[sum],dp[sum]);
}
return 0;
}