Go: ///
// File:  0529.go
// Author: ymiyamoto
//
// Created on Wed Feb 28 20:39:28 2018
//
package main

import (
	"fmt"
	"sort"
)

func max(n, m int64) int64 {
	if n < m {
		return m
	} else {
		return n
	}
}

func main() {
	for {
		var N int
		var M int64
		fmt.Scan(&N, &M)
		if N == 0 && M == 0 {
			break
		}

		arr := make([]int64, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })

		sums := make([]int64, len(arr)*len(arr))
		for i := 0; i < len(arr); i++ {
			for j := 0; j < len(arr); j++ {
				sums[i+j] = arr[i] + arr[j]
			}
		}
		sort.Slice(sums, func(i, j int) bool { return sums[i] < sums[j] })

		var m int64 = 0
		for i := 0; i < len(sums); i++ {
			rem := M - sums[i]
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, sums[i]+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
    int n;
    ll m;
    while(cin>>n>>m,n){
        vll v(n);
        rep(i,n){
            cin>>v[i];
        }
        sort(ALL(v));
        vll x;
        x.PB(0LL);
        rep(i,n){
            // x.insert(v[i]);
            x.PB(v[i]);
        }
        rep(i,n){
            rep(j,n){
                // x.insert(v[i]+v[j]);
                x.PB(v[i]+v[j]);
            }
        }
        sort(ALL(x));
        x.erase(unique(ALL(x)),x.end());
        // sort(ALL(x));
        ll ans=0;
        rep(i,x.size()){
            ll t=m-x[i];
            if(t<0) continue;
            auto it=upper_bound(ALL(x),t);
            if(it==x.begin()) continue;
            it--;
            maxs(ans,*it + x[i]);
        }
        cout<<ans<<endl;
    }
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
      cout<<fixed<<setprecision(20);
    mainmain();
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long int ll;

int main()
{
	const int MAX_N=105;
    int n,m,p[MAX_N];
	int x[MAX_N*MAX_N];
	while(true){
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)return 0;
		for(int i=0;i<n;i++)scanf("%d",&p[i]);
		p[n]=0;
		n++;

		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				x[i*n+j]=p[i]+p[j];
			}
		}
		sort(x,x+n*n);

		int ans=0;
		for(int i=0;i<n*n;i++){
			int mp=m-x[i];
			if(mp<0)continue;
			int l=0,r=n*n-1;
			int y=0;
			while(r-l>=1){
				int c=(r+l)/2;
				if(x[c]==mp){
					y=x[c];
					break;
				}
				else if(x[c]<mp){
					y=x[c];
					l=c+1;
				}
				else r=c;
			}
			ans=max(ans,y+x[i]);
		}
		printf("%d\n",ans);
	}
    return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
  int n, m;
  while(1){
    scanf("%d%d", &n, &m);
    if(n == 0 && m == 0)return 0;
    int res = 0, tmp;
    vector<int> points, points2;
    for(int i = 0;i < n;i++){
      scanf("%d", &tmp);
      if(tmp < m)points.push_back(tmp);
    }
    vector<int>::iterator it1,it2;
    for(it1 = points.begin();it1 != points.end();it1++){
      points2.push_back(*it1);
      for(it2 = points.begin();it2 != points.end();it2++){
	if(*it1 + *it2 <= m)points2.push_back(*it1 + *it2);
      }
    }
    sort(points2.begin(),points2.end());
    for(it1 = points2.begin();it1 != points2.end();it1++){
      it2 = upper_bound(points2.begin(), points2.end(), m - *it1);
      res = max(res, *it1);
      if(it2 == points2.begin())continue;
      it2--;
      res = max(res, *it1 + *it2);
    }
    printf("%d\n",res);
  }
}

C++: #include <iostream>
#include <algorithm>
#include <set>

using namespace std;

int main ( void )
{
	int n, m;
	int p[4];
	set<int> S;
	
	while ( cin >> n >> m, (n | m) )
	{
		int ans = 0;
		S.clear();
		S.insert(0);
		for ( int i = 0; i < n; ++i )
		{
			cin >> p[i];
			if ( p[i] < m )
			{
				S.insert(p[i]);
				ans = max( ans, p[i] );
			}
		}
		
		for ( int i = 0; i < n; ++i )
		{
			for ( int j = i; j < n; ++j )
			{
				int x = p[i] + p[j];
				if ( x <= m )
				{
					S.insert(x);
					ans = max( ans, p[i] );
				}
			}
		}
		
		if ( S.count(m) )
		{
			cout << m << endl;
			continue;
		}
		
		for ( int i = 0; i < n; ++i )
		{
			for ( int j = i; j < n; ++j )
			{
				int x = p[i] + p[j];
				if ( x <= m )
				{
					int y = m - x;
					while ( !S.count(y) && ( ans < (x + y) ) )
					{
						--y;
					}
					ans = max( ans, (x + y) );
				}
			}
		}
		
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
	int n,m,in,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&in);
			P.push_back(in);
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			if(P[i] >= m){
				break;
			}
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			MAX = max(MAX,P[i] + *it);
		}
	
		printf("%d\n",MAX);
	}
	
	return 0;
}

C++: #include<iostream>
using namespace std;

#define N 1000
#define M 200000000

typedef long long ll;

#define max(a,b) (a>b?a:b)

int main() {
	ll dp[N+1][5], ans;
	int n, m, a[N+1];
	while(1) {
		cin >> n >> m;
		if(!n&&!m) break;
		for(int i=1; i<=n; ++i) cin >> a[i];
		for(int i=0; i<=n; ++i) {
			for(int j=0; j<5; ++j) {
				dp[i][j] = 0;
			}
		}
		ans = 0;
		for(int i=1; i<=n; ++i) {
			for(int j=1; j<5; ++j) {
				dp[i][j] = dp[i-1][j];
				for(int k=1; k<=j; ++k) {
					if(dp[i-1][j-k]+a[i]*k<=m) {
						dp[i][j] = max(dp[i][j], dp[i-1][j-k]+a[i]*k);
					}
				}
			}
		}
		for(int i=1; i<5; ++i) ans = max(ans, dp[n][i]);
		cout << ans << endl;
	}
	return 0;
}