比1085多了一些变化,见代码。
My Code:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 250007;
struct node {
int val;
int num;
} a[55];
int c1[N], c2[N];
int main() {
//freopen("data.in", "r", stdin);
int n, i, j, sum, k;
while(cin >> n) {
if(n < 0) break;
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
for(i = 1; i <= n; ++i) {
scanf("%d%d", &a[i].val, &a[i].num);
}
for(i = 0; i <= a[1].num * a[1].val; i += a[1].val) {
c1[i] = 1; c2[i] = 0;
}
sum = a[1].num * a[1].val;
for(i = 2; i <= n; ++i) {
for(j = 0; j <= sum; ++j) {
for(k = 0; k <= a[i].num * a[i].val; k += a[i].val) { //here
c2[k+j] += c1[j];
}
}
for(j = 0; j <= sum + a[i].num * a[i].val; ++j) {
c1[j] = c2[j]; c2[j] = 0;
}
sum += a[i].num * a[i].val;
}
for(i = sum/2; i >= 0; --i)
if(c1[i] != 0) break;
printf("%d %d\n", sum - i, i);
}
return 0;
}