#include<cstdio>
#include<algorithm>

using namespace std;

int main()
{
	for(int n,m;scanf("%d%d",&n,&m),n;){
		int p[1001];	p[0]=0;
		for(int i=1;i<=n;i++)	scanf("%d",p+i);

		int nn=0;
		static int xp[1001*1001];
		for(int i=0;i<=n;i++)for(int j=i;j<=n;j++)	xp[nn++]=p[i]+p[j];
		sort(xp,xp+nn);

		int maxscore=0;
		for(int i=0;i<nn;i++){
			if(m-xp[i]>=0)
				maxscore=max(maxscore,xp[i]+upper_bound(xp,xp+nn,m-xp[i])[-1]);
		}
		printf("%d\n",maxscore);
	}

	return 0;
}