Description

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). 

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different. 
A test case starting with a negative integer terminates input and this test case is not to be processed. 

Output

For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B. 

Sample Input


2 10 1 20 1 3 10 1 20 2 30 1 -1


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int dp[150000],b[10000];
int main()
{
int N;
while(scanf("%d",&N)&&N>0)
{
int total=0,p=0;
int v,m;
for(int i=0;i<N;i++)
{
scanf("%d %d",&v,&m);
total += v*m;
for(int j=0;j<m;j++)
b[p++]=v;
}
memset(dp,0,sizeof(dp));
for(int i=0; i<p;i++)
for(int j=total/2;j>=b[i];j--)
dp[j]=max(dp[j],dp[j-b[i]]+b[i]);
printf("%d %d\n",max(dp[total/2],total-dp[total/ 2]),min(dp[total/2],total-dp[total/2]));
}
return 0;
}

Sample Output


20 10 40 40


题意:

一个学院要拆分为两个学院,给出学院的物体价值和数量,使拆分后的两个学院的价值尽量相等,不能相等的话就让第一个学院的价值多。

思路:

这道题就是一道01背包的变形,没有容量,把总价值/2,然后在去选择价值能达到最多但不超过总价值一半的作为第二个学院的价值

代码如下: