#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 1002 int cmp(const void *x,const void *y) { return *(int *)x-*(int *)y; } int Max(int x,int y) { if(x>y) return x; else return y; } int main() { int n,i,j,m; int price[MAXN],dp[MAXN]; while(scanf("%d",&n)&&n) { memset(dp,0,sizeof(dp)); for(i=0;i<n;++i) scanf("%d",&price[i]); qsort(price,n,sizeof(price[0]),cmp); scanf("%d",&m); if(m<5) { printf("%d\n",m); continue; } m-=5;//用5元购买最贵的菜 for (i=0;i<n-1;++i) { for(j=m;j>=price[i];--j) dp[j]=Max(dp[j],dp[j-price[i]]+price[i]); } printf("%d\n",m+5-dp[m]-price[n-1]); } return 0; }