你只有非常努力,才能看起来毫不费力。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f
const int maxn=1000+10;
int dp[maxn];
int cost[maxn];
int V;
int main()
{
int n,i,j,k;
while(EOF!=scanf("%d",&n)&&n)
{
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
scanf("%d",&cost[i]);
sort(cost+1,cost+n+1);
cin >> V;
if(V<5||n<=0){
cout << V << endl;
continue;
}
dp[V]=1;
int pos=V;//记录最大1出现的位置
for(i=1;i<=n-1;i++){
if(pos-cost[i]<5) j=5;
else j=pos-cost[i];
for(j;j<=V-cost[i];j++){
if(0==dp[j]&&dp[j+cost[i]]){
dp[j]=1;
pos=min(pos,j);
}
}
}
cout << pos-cost[n] << endl;
}
return 0;
}