详解见:http://bonniebbs.is-programmer.com/posts/40073.html
#include <iostream> #include <cstring> #include <cstdio> #define N 250005 #define num 55 using namespace std; int main() { int n, v, m, t, s, ss, max; int c[2][N]; int in[2][num]; while(scanf("%d", &n), n>=0) { memset(c, 0, sizeof(c)); memset(in, 0, sizeof(in)); for(int i=1; i<=n; i++) scanf("%d %d", &in[0][i], &in[1][i]);//v m s = 0; max = in[0][1]*in[1][1]; for(int i=0; i<=max; i+=in[0][1]) { c[s][i] = 1; } for(int i=2; i<=n; i++) { for(int j=0; j<=max; j++) { if(c[s][j]) for(int k=0; k<=in[0][i]*in[1][i]; k+=in[0][i]) { c[1-s][k+j] += c[s][j]; } } max += in[0][i]*in[1][i]; s = 1-s; } t = (max+1)/2; while(c[s][t]==0) t++; cout << t <<" " << max-t << endl; } return 0; }