#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

const int MAX_N = 1009;
int ps[MAX_N];
int N, M;

int solve(){
	ps[N++] = 0;
	vector<int> as;
	for(int i=0; i<N; i++)for(int j=i; j<N; j++)if(ps[i]+ps[j] <= M){
		as.push_back(ps[i] + ps[j]);
	}
	sort(as.begin(), as.end());
	as.erase(unique(as.begin(), as.end()), as.end());

	int ret = 0;
	for(int i=0; i<as.size(); i++){
		ret = max(ret, as[i] + *lower_bound(as.rbegin(), as.rend(), M-as[i], greater<int>()));
	}
	return ret;
}

int main(){
	while(scanf("%d%d",&N,&M),N){
		for(int i=0; i<N; i++){
			scanf("%d",ps+i);
		}
		printf("%d\n",solve());
	}
	return 0;
}