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

import (
	"fmt"
	"sort"
)

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

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

		arr := make([]int, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Ints(arr)

		sums := make([]int, 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.Ints(sums)

		m := 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<stdio.h>
#include<algorithm>

using namespace std;
int main(){
	int n,m,ans;
	int dp[4][1001];
	for(;;){
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)break;
		for(int i=0;i<1001;i++)for(int j=0;j<4;j++)dp[j][i]=0;
		for(int i=0;i<n;i++)scanf("%d",&dp[0][i]);
		sort(dp[0],dp[0]+n);
		ans = 0;
		for(int i=0;i<n;i++){
			for(int j=1;j<4;j++){
				for(int k=0;k<n;k++){
					int t = dp[0][i]+dp[j-1][k];
					if(t<=m)dp[j][k] = max(dp[j][k], t);
				}
			}
		}
		for(int i=0;i<4;i++){
			for(int j=0;j<n;j++){
				ans = max(ans,dp[i][j]);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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));
        set<ll> se;
        se.insert(0LL);
        rep(i,n){
            se.insert(v[i]);
        }
        rep(i,n){
            rep(j,n){
                se.insert(v[i]+v[j]);
            }
        }
        vll x;
        for(ll t:se){
            x.PB(t);
        }
        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<bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
int main(void){
    ll n, m, p[1000010];
    while(cin>>n>>m){
        if(n+m==0) break;
        p[0]=0;
        ll maxp=0;
        for(ll i=1; i<=n; i++){
            cin >> p[i];
        }
        ll now=n+1;
        for(ll i=0; i<=n; i++){
            for(ll j=i; j<=n; j++){
                p[now++]=p[i]+p[j];
            }
        }
        sort(p, p+now);
        ll ans=0;
        for(int i=0; i<now; i++){
        ll maxa=m-p[i];
        if(maxa<0) continue;
        ll under=0, upper=now;
        while(upper-under>0){
            ll mid=(under+upper)/2;
            if(maxa>p[mid]){
                under=mid+1;
            }
            else if(maxa==p[mid]){
                break;
            }
            else{
                upper=mid;
            }
        }
        ans=max(ans, p[i]+p[upper-1]);
        }
        cout << ans << "\n";
         
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
	int n, m;
	int p;
	vector<int> g;
	while (1){
		cin >> n >> m;
		if (n == 0 && m == 0)break;
		for (int i = 0; i < n; i++){
			cin >> p;
			g.push_back(p);
		}
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){
				g.push_back(g[i] + g[i2]);
			}
		}
		int x, y;
		x = 0; y = g.size() - 1;
		int maxv = 0;
		sort(g.begin(), g.end());
		while (1){
			if (g[x] + g[y] > maxv&&g[x] + g[y] <= m)maxv = g[x] + g[y];
			if (g[x] + g[y] <= m)x++;
			if (g[x] + g[y] > m)y--;
			if (x > y)break;
		}
		cout << maxv << endl;
		g.clear();
	}
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
# define ALL(x)     (x).begin(),(x).end()
# define UNIQ(c)    (c).erase(unique(ALL((c))), end((c)))
# define mp         make_pair
# define eb         emplace_back
# define FOR(i,a,b) for(int i=(a);i<(b);++i)
# define REP(i,n)   FOR(i,0,n)
# define INIT       std::ios::sync_with_stdio(false);std::cin.tie(0);

int p[1001];

int main() {
	int n, m;
	while (cin >> n >> m&&n&&m) {
		REP(i, n)cin >> p[i];
		vector<int> vec;
		REP(i, n + 1)REP(j, n + 1) {
			vec.emplace_back(p[i] + p[j]);
		}
		sort(vec.begin(), vec.end());
		int ans = 0;
		REP(i, vec.size()) {
			int num = m - vec[i];
			if (num < 0)continue;
			int num2 = upper_bound(vec.begin(), vec.end(), num) - 1 - vec.begin();
			ans = max(vec[i] + vec[num2], ans);
		}
		cout << ans << endl;
	}
}