#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define MP make_pair

typedef vector<int> vi;

int main() {
	while (true) {
		int n, m, score[1001];
		vi v;
		cin >> n >> m;
		if (n == 0 && m == 0) break;
		REP(i, n) scanf("%d", &score[i]);
		score[n++] = 0;

		REP(i, n) FOR(j, i, n) v.push_back(score[i] + score[j]);
		sort(v.begin(), v.end(), greater<int>());

		int ans = 0;
		REP(i, n) FOR(j, i, n) {
			vi::iterator it = lower_bound(v.begin(), v.end(), m - score[i] - score[j], greater<int>());
			if (it == v.end()) continue;
			ans = max(ans, *it + score[i] + score[j]);
		}

		cout << ans << endl;
	}
	return 0;
}