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 <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using vs=vector<string>;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF    	((ll)1ll<<60)
#define INF     	((int)1<<30)
#define EPS     	(1e-9)
#define MOD     	(1000000007ll)
#define fcout(a)	cout<<setprecision(a)<<fixed
#define fs 			first
#define sc			second
#define PI			3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s; a=0;} return os;}
void OUT(bool b){cout<<(b?"YES":"NO")<<endl;}
void Out(bool b){cout<<(b?"Yes":"No")<<endl;}
void out(bool b){cout<<(b?"yes":"no")<<endl;}
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int clz(int x) { return 31 - __builtin_clz(x); }
int clz(ll x) { return 63 - __builtin_clzll(x); }
int ctz(int x) { return __builtin_ctz(x); }
int ctz(ll x) { return __builtin_ctzll(x); }

class compare {
public:
	bool operator()(tuple<ll, ll> a, tuple<ll, ll> b) {
		return (get<1>(a) > get<1>(b));
	}
};

void solve(ll n,ll m){
	ll ans=0;
	vl v(n);
	rep(i,n){
		cin>>v[i];
	}
	v.push_back(0);
	vl cop;
	rep(i,n+1){
		range(j,i,n+1){
			cop.push_back(v[i]+v[j]);
		}
	}
	sort(all(cop));
	for(auto x:cop){
		ll y=m-x;
		if(y<0)continue;
		auto itr=upper_bound(all(cop),y);
		if(*itr!=0)itr--;
		ans=max(*itr+x,ans);
	}
	cout<<ans<<endl;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n,m;
	while(cin>>n>>m,n){
		solve(n,m);
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(int i=a; i<int(b); i++)
#define rep(i,b) loop(i,0,b)
#define each(e,c) for(auto& e:c)
#define iter(it,c) for(auto it=c.begin(); it!=c.end(); ++it)

ll const inf = 1<<28;
double const pi = acos(-1);

int a[1024];
int d[1024*1024];

int solve(int n, int k){
    int c=0;
    rep(i,n)loop(j,i,n){
        int s=a[i]+a[j];
        if(s<=k){
            d[c++]=s;
        }
    }

    sort(d,d+c);
    int ans=0;
    rep(i,c){
        int s1=d[i];
        int lo=0, hi=c;
        while(lo+1<hi){
            int mid=(lo+hi)>>1;
            int s2=d[mid];
            if(s1+s2>k){
                hi=mid;
            }else{
                lo=mid;
            }
        }
        int s2=d[lo];
        ans=max(ans, s1+s2);
    }
    return ans;
}

int main(){
    int n,k;
    while(cin>>n>>k && n|k){
        a[0]=0;
        rep(i,n)cin>>a[i+1];
        n++;
        cout << solve(n,k) << endl;
    }
}

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


#define rep(X, Y) for(int (X) = 0; (X) < (Y); ++(X))
#define MAX 1001

typedef long long ll;

int n;
ll m, b[MAX], h[MAX*MAX], res = -1;

int main(){
	while(scanf("%d%lld", &n, &m), n|m){

		rep(i, n){
			scanf("%lld", b + i + 1);
		}
		++n;

		rep(i, n){
			rep(l, n){
				h[i * n + l] = b[i] + b[l];
			}
		}

		std::sort(h, h + n * n);

		res = -1;
		rep(i, n*n){
			auto it = std::upper_bound(h, h + n * n, m - h[i]);

			if( it - 1 >= h ) res = std::max(res, *(it - 1) + h[i]);
		}

		printf("%lld\n", res);
	}

	return 0;
}

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


using namespace std;
static const double EPS = 1e-6;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
#ifndef M_PI
const double M_PI=acos(-1);
#endif
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int((a).size()))
#define F first
#define S second
int dx[]={0,1,0,-1,1,1,-1,-1},dy[]={1,0,-1,0,1,-1,1,-1};

int n,m;
int in[1000];

void solve(){
  int ans = 0;
  rep(i,n){
    cin >> in[i];
    if(in[i] <= m) ans = max(in[i],ans);
  }
  sort(in,in+n);

  vector<int> dbl;
  rep(i,n)rep(j,i+1){
    int su = in[i] + in[j];
    dbl.pb(su);
    if(su <= m) ans = max(su,ans);
  }
  sort(ALL(dbl));

  int idx = SZ(dbl) - 1;
  rep(i,n){
    while(idx > 0 && dbl[idx]+in[i]>m) --idx;
    if(idx>=0 && dbl[idx]+in[i] <= m) ans = max(dbl[idx]+in[i],ans);
  }

  idx = SZ(dbl) - 1;
  rep(i,SZ(dbl)){
    while(idx > 0 && dbl[idx]+dbl[i]>m) --idx;
    if(idx>=0 && dbl[idx]+dbl[i] <= m) ans = max(dbl[idx]+dbl[i],ans);
  }

  cout << ans << endl;
}

main(){
  while(cin>>n>>m,n|m) solve();
}

C++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>
using namespace std;


int main(){
	
	int n,m;
	while(1){
		cin>>n>>m;
		if(!n&&!m)break;
		int g[1009]={};
		int w=2000000009;
		for(int i=0;i<n;i++)
		{cin>>g[i];w=min(w,g[i]);}
		sort(g,g+n);
		reverse(g,g+n);
		long long int ans=0,H=0;;
		int y=0;
		int r=3;
		int i=0;
		while(i<4){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
			if(y==n)break;
		}
		y=0;
		H=max(H,ans);
		r=2;i=0;ans=0;
		while(i<3){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
			if(y==n)break;
		}
		
		y=0;
		H=max(H,ans);
		r=1;i=0;ans=0;
		while(i<2){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
			if(y==n)break;
		}
		
		y=0;
		H=max(H,ans);
		r=0;i=0;ans=0;
		while(i<1){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
			if(y==n)break;
		}
		H=max(H,ans);


		cout<<H<<endl;
	}
	


	return 0;
}