#include<iostream>
#include<algorithm>
using namespace std;
int s[501501],a[1001],n,m,t;
int BNS(int key){
	int ret=0,ran=t/2,nse=0;
	while(true){
		nse+=ran;
		if(s[nse]<=key)ret=max(ret,nse);
		else nse-=ran;
		ran/=2;
		if(!ran)break;
	}
	return ret;
}
int main(){
	while(true){
	cin>>n>>m;
	if(!n&&!m)break;
	a[0]=0,t=1,s[0]=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		for(int j=0;j<=i;j++)if(a[i]+a[j]<=m)s[t]=a[i]+a[j],t++;
	}
	sort(s,s+t);
	int ans=0;
	for(int i=0;i<t;i++)ans=max(ans,s[i]+s[BNS(m-s[i])]);
	cout<<ans<<endl;
	}
	return 0;
}