#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
	int n,m;
	while(1){
		cin>>n>>m;
		if(n==0)break;
		int ans=0;
		vector<int> a,sum;
		a.push_back(0);
		for(int i=1;i<=n;i++){
			int s;
			cin>>s;
			a.push_back(s);
		}
		sort(a.begin(),a.end());
		for(int i=0;i<=n;i++){
			for(int j=i;j<=n;j++){
				if(a[i]+a[j]<=m)sum.push_back(a[i]+a[j]);
			}
		}
		sort(sum.begin(),sum.end());
		int suml=sum.size()-1;
		for(int i=0;i<=suml;i++){
			while(sum[i]+sum[suml]>m)suml--;
			if(ans<sum[i]+sum[suml])ans=sum[i]+sum[suml];
		}
		cout<<ans<<endl;
	}
}