#include<bits/stdc++.h>
#define rep(i,n)for(ll i=0;i<n;i++)
using namespace std;
typedef long long ll;

ll k[1000010];
ll p[1010];
int main() {
	ll n, m;
	while (scanf("%lld%lld", &n, &m), n) {
		memset(p, 0, sizeof(p));
		rep(i, n)scanf("%lld", &p[i]);
		ll c = 0;
		rep(i, n + 1)for (ll j = i; j < n + 1; j++) {
			k[c++] = p[i] + p[j];
		}
		sort(k, k + c);
		ll Max = 0;
		rep(i, n + 1)for (ll j = i; j < n + 1; j++) {
			ll*a = upper_bound(k, k + c, m - p[i] - p[j]);
			if (a == k)continue;
			Max = max(Max, *(a - 1) + p[i] + p[j]);
		}
		printf("%lld\n", Max);
	}
}