题目 http://acm.pku.edu.cn/JudgeOnline/problem?id=1276 #include <stdio.h> #include <memory> #define max_n 200 #define max_cash 1000001 #define MAX(x,y) ((x) > (y) ? (x) : (y)) int c[max_n]; int f[max_cash]; int k; int cash,N; int dp(){ memset(f,0,sizeof(f)); int i,v; for(i = 1;i <= k;i++) for(v = cash;v>=c[i];v--) f[v] = MAX(f[v],f[v - c[i]] + c[i]); return f[cash]; } int main(){ int i; int sum,d,mi; //freopen("data.txt","r",stdin); while(scanf("%d%d",&cash,&N) != EOF){ k = 1; memset(c,0,sizeof(c)); for(i = 0;i < N;i++){ scanf("%d%d",&sum,&d); mi = 1; while(true){ if(sum <= mi){ c[k++] = d * sum; break; } else{ c[k++] = d * mi; sum -= mi; mi *= 2; } } } k--; printf("%d/n",dp()); } return 0; }