"use strict"
function KP(callback) {
    this.data = [];
    this.reader = require('readline').createInterface({
        input: process.stdin,
        output: process.stdout
    });
    this.id = 0;
    this.start = function(callback) {
        self.reader.on('line', function(line) {
            var dt = line.split(" ");
            for (var i = 0; i < dt.length; ++i) {
                self.data.push(dt[i]);
            }
        });
        self.reader.on('close', function() {
            callback(self);
        });
    }
    this.input = function() {
        var id = self.id++;
        if (id >= self.data.length) {
            self.error("reached eof");
        }
        return self.data[id];
    }
    this.output = function(data) {
        if (arguments.length > 1) {
            data = [];
            for (var i = 0; i < arguments.length; ++i) {
                data[i] = arguments[i];
            }
        }
        else if (typeof data !== "object") {
            data = [data];
        }
        console.log(data.join(" "));
    }
    this.error = function(err) {
        console.error(err);
        console.abort();
    }
    var self = this;
};

new KP().start(function(kp) {
    while (true) {
        var n = +kp.input();
        var M = +kp.input();
        if (n == 0 && M == 0) break;
        var a = [];
        for (var i = 0; i < n; ++i) {
            a.push(+kp.input());
        }
        var ans = 0;
        var update_ans = function(val) {
            if (val > M) return;
            ans = Math.max(ans, val);
        };
        var v = [0];
        for (var i = 0; i < n; ++i) {
            v.push(a[i]);
            for (var j = 0; j < n; ++j) {
                v.push(a[i]+a[j]);
                update_ans(a[i]+a[j]);
            }
        }
        v = v.sort();
        for (var i = 0; i < v.length; ++i) {
            var l = 0, r = v.length;
            for (var t = 0; t < 50; ++t) {
                var m = Math.floor((l + r) / 2);
                if (v[i] + v[m] <= M) {
                    l = m;
                }
                else {
                    r = m;
                }
            }
            update_ans(v[i]+v[l]);
        }
        kp.output(ans);
    }
    process.exit(0);
});