#include <cstdio> #include <iostream> #include <string> #include <cstring> #include <algorithm> #include <cmath> #define PI acos(-1.0) #define ms(a) memset(a,0,sizeof(a)) #define msp memset(mp,0,sizeof(mp)) #define msv memset(vis,0,sizeof(vis)) #define msd memset(dp,0,sizeof(dp)) using namespace std; //#define LOCAL bool cmp(int a,int b) { return a>b; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); //freopen("out.txt","w",stdout); #endif // LOCAL ios::sync_with_stdio(false); int n; while(cin>>n&&n) { int v[2000]; for(int i=0; i<n; i++) cin>>v[i]; int m;//容量 cin>>m; sort(v,v+n,cmp); if(m<5)printf("%d\n",m);//买不了 else { int dp[2000]; msd; for(int i=1; i<n; i++) for(int j=m-5; j>=v[i]; j--) if(dp[j]<dp[j-v[i]]+v[i]) dp[j]=dp[j-v[i]]+v[i]; printf("%d\n",m-v[0]-dp[m-5]); } } return 0; }