#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int N, M;
int score[1002001];
int point[1001];
int bin_search(int n){
    int h=0, t=(N+1)*(N+1);
    int temp;
    int status;
    while(h<=t){
        temp = (t+h)/2;
        if(n == score[temp]) return -1;
        else if(n > score[temp]){
            h = temp + 1;
            status = 1;
        }else{
            t = temp - 1;
            status = 0;
        }
    }
    if(status){
        return score[temp];
    }else{
        return score[temp-1];
    }
}
int main(){
    int a;
    int size;
    int b;
    int m;
    cin >> N >> M;
    while(N || M){
        m = 0;
        point[0] = 0;
        for(int i=1;i<=N;i++){
            cin >> point[i];
        }
        size = -1;
        for(int i=0;i<=N;i++){
            for(int j=0;j<=N;j++){
                score[size++] = point[i]+point[j];
            }
        }
        sort(score,score+size);
        for(int i=0;i<size && score[i] < M;i++){
            b = bin_search(M-score[i]);
            if(b == -1){
                break;
            }else{
                m = max(m, b+score[i]);
            }
        }
        cout << m << endl;
        cin >> N >> M;
    }
}
#include<iostream>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<cstdlib>
#include<typeinfo>
#include<sstream>
#include<list>
#include<set>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define fori(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
int in()
{
	int x;cin>>x;
	return x;
}
int stringInt(string a)
{
	return atoi(a.c_str());
}
string intString(int a)
{
	stringstream ss;
	ss<<a;
	return ss.str();
}
vector<int> SieveOfEratosthenes(int x)
{
	vector<int> a;
	vector<int> b(x+1,0);
	for(int i=2;i<=x;i++)
	{
		if(b[i]==0)
		{
			a.push_back(i);
			for(int l=i;l<=x;l+=i)
			{
				b[l]=1;
			}
		}
	}
	return a;
}

int main ()
{
	int n,m;
	while(cin>>n>>m&&n&&m)
	{
		vector<long long > a,b;
		b.push_back(0);
		rep(i,n)
		{
			b.push_back(in());
		}
		rep(i,n+1)
		{
			rep(l,n+1)
			{
				a.push_back(b[i]+b[l]);
			}
		}
		long long ans = 0;
		sort(a.begin(),a.end());
		rep(i,a.size())
		{
			vector<long long>::iterator t=upper_bound(a.begin(),a.end(),m-a[i]);
			if(t==a.begin())continue;
			t--;
			ans=max(ans,*t+a[i]);
		}
		cout<<ans<<endl;


	}

}