#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int qq=1000+10; int price[qq<<1],dp[qq<<1]; int main() { int n; while(~scanf("%d",&n)&&n){ memset(price,0,sizeof(price)); //初始化 memset(dp,0,sizeof(price)); for(int i=0;i<n;++i) scanf("%d",&price[i]); sort(price,price+n); int money;scanf("%d",&money); if(money<5){ printf("%d\n",money); continue; } money=money-5; //取出5元用于购买最贵的物品、 for(int j,i=0;i<n-1;++i) for(j=money;j>=price[i];--j) dp[j]=max(dp[j],dp[j-price[i]]+price[i]); //物品只有买或者不买两种选择 printf("%d\n",money+5-dp[money]-price[n-1]); } return 0; }
时间: 2024-10-09 21:10:01