#include <bits/stdc++.h>

using namespace std;

#define int long long

#define rep(i,x) for(int i=0;i<x;++i)
#define all(a) begin(a),end(a)
#define PB push_back

int P[1010];

signed main()
{
    int N, M;
    
    while (cin >> N >> M, N) {
        rep(i, N) {
            cin >> P[i];
        }

        vector<int> arr;

        rep(i, N) {
            arr.PB(P[i]);

            rep(j, i+1) arr.PB(P[i] + P[j]);
        }

        arr.PB(0);
        sort(all(arr));

        int ma = 0;

        rep(i, arr.size()) {
            int a = arr[i];
            int b = *(lower_bound(all(arr), M - a + 1) - 1);
            if (a + b <= M) ma = max(ma, a + b);
        }

        cout << ma << endl;
    }
}