HDU 2546 饭卡
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2546
#include <iostream> using namespace std; int dp[1010]={0}; void ZeroOnePack(int cost,int weight,int V) { for(int i=V-5;i>=cost;i--) dp[i]=max(dp[i],dp[i-cost]+weight); } int main(int argc, const char *argv[]) { int n; //freopen("input.txt","r",stdin); while(cin>>n&&n) { int w[1010]={0}; int fmax = 0; int idx = 0; int V; memset(dp,0,sizeof(dp)); memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) { cin>>w[i]; if(w[i]>fmax) { fmax = w[i]; idx=i; } } cin>>V; if(V<5) cout<<V<<endl; else { for(int i=0;i<n;i++) { if(i!=idx) { ZeroOnePack(w[i],w[i],V); } } cout<<V-dp[V-5]-fmax<<endl; } } return 0; }