分析:母函数(详细见找单词)
代码:
#include<cstdio>
#include<cstring>
const int maxn = 300000;
int c1[maxn], c2[maxn], v[110], m[110];
int main(){
int n;
while(scanf("%d", &n), n > 0){
int sum = 0;
for(int i = 1; i <= n; i++){
scanf("%d%d", &v[i], &m[i]);
sum += v[i]*m[i];
}
memset(c1, 0, sizeof(c1));
memset(c2, 0, sizeof(c2));
c1[0] = 1;
for(int i = 1; i <= n; i++){
for(int j = 0; j <= m[i]; j++)
for(int k = j*v[i]; k <= sum; k++)
c2[k] += c1[k - j*v[i]];
memcpy(c1, c2, sizeof(c2));
memset(c2, 0, sizeof(c2));
}
for(int i = (sum + 1)/2; i <= sum; i++)
if(c1[i]){
printf("%d %d\n", i, sum - i);
break;
}
}
return 0;
}