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
2
10 1
20 1
3
10 1
20 2
30 1
-1
output
20 10
40 40
code
//Siberian Squirrel
//#include<bits/stdc++.h>
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#define ACM_LOCAL
using namespace std;
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
const int MOD = 3221225473;
const int N = 3e5 + 10;
const int UP = 110;
int f1[N], f2[N];
int v, m;
inline void solve(int n, int res = 0) {
memset(f1, 0, sizeof f1);
f1[0] = 1;
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &v, &m);
res += v * m;
for (int j = 0; j <= res; ++j) {
for (int k = 0; j + k * v <= res && k <= m; ++k) {
f2[j + k * v] += f1[j];
}
}
for (int j = 0; j <= res / 2; ++j) {
f1[j] = f2[j];
f2[j] = 0;
}
}
for (ll i = res / 2; i >= 0; --i) {
if (f1[i]) {
printf("%d %d\n", res - i, i);
break;
}
}
}
int main() {
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
#endif
int o = 1, n, m, v;
// scanf("%d", &o);
while(o --) {
while(scanf("%d", &n) && n > 0) {
solve(n);
}
}
return 0;
}