#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
using LL = long long;

int main() {
    int N, M, P[1002];

    while (cin >> N >> M) {
        if ((N | M) == 0) break;

        P[0] = 0;
        for (int i = 1; i <= N; i++) cin >> P[i];

        vector<LL> S;
        for (int i = 0; i <= N; i++) {
            for (int j = 0; j <= N; j++) {
                S.push_back(P[i] + P[j]);
            }
        }
        sort(S.begin(), S.end());

        LL ans = 0;
        for (auto s : S) {
            if (M - s > 0) {
                ans = max(ans, s + *(lower_bound(S.begin(), S.end(), M - s) - 1));
            }
        }

        cout << ans << endl;
    }
}

