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
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define maxn 55
#define max_val 1250005
using namespace std;
struct list
{
int val;
int num;
}an[maxn];
int dp[max_val];
int main()
{
int n;
while(scanf("%d",&n),n>=0)
{
int sum=0;
memset(an,0,sizeof(an));
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
scanf("%d%d",&an[i].val,&an[i].num);
sum+=an[i].val*an[i].num;
}
for(int i=0;i<n;i++)
for(int k=1;k<=an[i].num;k++)
for(int j=sum/2;j>=k*an[i].val;j--)
dp[j]=max(dp[j],dp[j-k*an[i].val]+k*an[i].val);
printf("%d %d\n",sum-dp[sum/2],dp[sum/2]);
}
return 0;
}