The idea is to give some things and their value, and then divide those things into two parts of equal or most similar value.
Reference article:
Introduction to Generating Function: http://www.wutianqi.com/?p=596
Advanced Generating Function: https://blog.csdn.net/xiaofei_it/article/details/17042651
Answer to this question: https:// blog.csdn.net/xiaofei_it/article/details/17041709
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 250010;
int a[maxn],b[maxn],v[50],m[50],i,last,last2,n;
int main(){
while(scanf("%d",&n)&&n>0){
for(i=0;i<n;i++) scanf("%d%d",&v[i],&m[i]);
a[0]=1;
last=0;
for(i=0;i<n;i++){
last2=last+v[i]*m[i];
memset(b,0,sizeof(int)*(last2+1));
for(int j=0;j<=m[i];j++) for(int k=0;k<=last;k++) b[k+j*v[i]]+=a[k];
memcpy(a,b,sizeof(int)*(last2+1));
last=last2;
}
for(i=last/2;i>=0&&a[i]==0;i--);
printf("%d %d\n",last-i,i);
}
return 0;
}