Big Event in HDU - http://acm.hdu.edu.cn/showproblem.php?pid=1171
代码
#include<bits/stdc++.h>
using namespace std;
#define MXN 250010
int main(){
int N, M, c1[MXN], c2[MXN], v[51], vn[51];
while(scanf("%d", &N)){
if(N < 0) break;
memset(c1, 0, sizeof c1);
memset(c2, 0, sizeof c2);
for(int i = 0; i < N; i++) scanf("%d %d", v+i, vn+i);
for(int i = 0; i <= vn[0]*v[0]; i += v[0]) c1[i] = 1;
M = vn[0]*v[0];
for(int i = 1; i <= N-1; i++){
for(int j = 0; j <= M; j++){
for(int k = 0; k <= v[i]*vn[i]; k += v[i]){
c2[j+k] += c1[j];
}
}
M += v[i]*vn[i];
for(int j = 0; j <= M; j++) c1[j] = c2[j], c2[j] = 0;
}
int mid = M/2;
while(c1[mid] == 0) mid--;
printf("%d %d\n", M - mid, mid);
}
return 0;
}