#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 12885;
int n,m;
int dp[maxn],v[maxn],p[maxn];
int main()
{
freopen("data.txt","r",stdin);
while( scanf("%d%d",&n,&m) != EOF ){
memset(dp,0,sizeof(dp));
for( int i = 0; i < n; i ++ )
scanf("%d%d",&v[i],&p[i]);
for( int i = 0; i < n; i ++ )
for( int j = m; j >= v[i]; j -- )
dp[j] = max( dp[j],dp[j-v[i]]+p[i] );
printf("%d\n",dp[m]);
}
return 0;
}
hdu-2546 饭卡
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int n,m;
int dp[maxn],p[maxn];
int main()
{
//freopen("data.txt","r",stdin);
while( scanf("%d",&n) != EOF,n ){
for( int i = 0; i < n; i ++ )
scanf("%d",&p[i]);
sort(p,p+n);
scanf("%d",&m);
if( m >= 5 ){
memset( dp,0,sizeof(dp) );
for( int i = 0; i < n-1; i ++ )
for( int j = m-5; j-p[i] >= 0; j -- )
dp[j] = max( dp[j],dp[j-p[i]] + p[i] );
printf("%d\n",m-dp[m-5]-p[n-1]);
}
else
printf("%d\n",m);
}
return 0;
}