緇欏畾涓瀹氭暟閲忕殑涓嶅悓闈㈠肩殑閽炵エ錛岃緭鍑虹敱榪欎簺閽炵エ緇勬垚鐨勪笉瓚呰繃鍑烘涓婇檺錛堥鐩腑鐨刢ash錛夌殑鏈澶ч噾棰濄?br />01鑳屽寘闂錛岃鍙傞槄錛?a href="http://www.shnenglu.com/hoolee/archive/2012/08/14/187179.html">http://www.shnenglu.com/hoolee/archive/2012/08/14/187179.html
榪欓噷鎴戞兂澶氳涓鍙ワ紝鏈涓儗鍖呯殑瀹歸噺鏄涓粰鐨刢ash錛屾瘡浠剁墿鍝佺殑鑺辮垂灝辨槸璇ラ挒紲ㄧ殑闈㈠鹼紝鐗╁搧鐨勪環鍊間篃鏄縐嶉挒紲ㄧ殑闈㈠鹼紝榪欓噷鐨勮姳璐瑰拰浠峰兼槸涓鏍風殑銆?br />浠ヤ笅鏄湰棰樹唬鐮侊細


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LENND 20
#define LENF 100010
typedef struct
{
int v;
int amt;
}Cash;
Cash cs[100];
int f[LENF];
int Max(int a, int b)
{
if(a > b)
return a;
return b;
}
int main()
{
int i, j;
int cash, N;
int n[LENND], D[LENND];
while(scanf("%d", &cash) != EOF)
{
scanf("%d", &N);
for(i = 0; i < N; i++)
scanf("%d%d", &n[i], &D[i]);
int count = 1;
for(i = 0; i < N; i++)
{
int M = n[i];
int t = 1;
while(M - t > 0)
{
cs[count].v = D[i];
cs[count++].amt = t;
M -= t;
t *= 2;
}
if(M != 0)
{
cs[count].v = D[i];
cs[count++].amt = M;
}
}
memset(f, 0, sizeof(f));
for(i = 1; i < count; i++)
{
int allv = cs[i].amt * cs[i].v;
for(j = cash; j >= allv; j--)
f[j] = Max(f[j], f[j - allv] + allv);
}
printf("%d\n", f[cash]);
}
//system("pause");
}

]]>