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*len(arr)+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 <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
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;
}


#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 MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int n,m;
	while(cin>>n>>m,n||m){
		// cout<<n<<" "<<m<<endl;
		vint v(n);
		rep(i,n){
			cin>>v[i];
		}
		v.PB(0);
		// set<int> se;
		vint se;
		rep(i,n+1){
			rep(j,n+1){
				// se.insert(v[i]+v[j]);
				se.PB(v[i]+v[j]);
			}
		}
		sort(ALL(se));
		se.erase(unique(ALL(se)),se.end());
		// cout<<(int)se.size()<<endl;
		// se.insert(0);
		int ans=0;
		for(auto i:se){
			if(i>m) break;
			auto it=upper_bound(ALL(se),m-i);
			it--;
			ans=max(ans,i+*it);
		}
		cout<<ans<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

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

using namespace std;

const int MAX_N=1000;

int N,M;
int P[MAX_N];

int comb[MAX_N*MAX_N];

vector<int> out;

int main(){
	do{
		scanf("%d %d",&N,&M);
		if (N!=0 || M!=0){
			int i,j;
			for (i=0;i<N;i++){
				scanf("%d",&P[i]);
			}
			for (i=0;i<N;i++){
				for (j=0;j<N;j++){
					comb[i*N+j]=P[i]+P[j];
				}
			}
			sort(comb,comb+N*N);
			int ma=0;
			//???????????´???
			for (i=0;i<N;i++){
				if (P[i]<=M){
					ma=max(ma,P[i]);
				}
			}
			//???????????´???
			int *p;
			p=upper_bound(comb,comb+N*N,M);
			if (p!=comb){
				ma=max(ma,*(p-1));
			}
			//???????????´???
			for (i=0;i<N;i++){
				p=upper_bound(comb,comb+N*N,M-P[i]);
				if (p!=comb){
					ma=max(ma,P[i]+*(p-1));
				}
			}
			//???????????´???
			for (i=0;i<N;i++){
				for (j=0;j<N;j++){
					p=upper_bound(comb,comb+N*N,M-P[i]-P[j]);
					if (p!=comb){
						ma=max(ma,P[i]+P[j]+*(p-1));
					}
				}
			}
			out.push_back(ma);
		}
	}while (N!=0 || M!=0);
	for (int i=0;i<out.size();i++){
		printf("%d\n",out[i]);
	}
	return 0;
}

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

using namespace std;

int main() {
	int n, m;

	while (cin >> n >> m, n && m) {
		vector<int> point(n + 1);
		vector<int> sum;
		for (int i = 0; i < n; ++i) {
			cin >> point[i];
		}
		for (int i = 0; i < n + 1; ++i) {
			for (int j = i; j < n + 1; ++j) {
				sum.push_back(point[i] + point[j]);
			}
		}
		sort(sum.begin(), sum.end());

		int ans = 0;
		for (int i = 0; i < sum.size(); ++i) {
			int t = m - sum[i];
			cout << "t = " << t << endl;
			vector<int>::iterator it = upper_bound(sum.begin(), sum.end(), t);
			if (it != sum.begin()) {
				ans = max(ans, sum[i] + * (it - 1));
			}
		}
		cout << ans << endl;
	}
}

C++: #include<algorithm>
#include<vector>

#define MAX_N 1001

using namespace std;

class CNum{
public:
	vector<int> v;
};

bool Find(vector<int> &v, int search){
	for(unsigned int i = 0; i < v.size(); i++){
		if( v[i] == search ){
			return true;
		}
	}
	return false;
}

int main(void){
	while( 1 ){
		int n, m;
		int V[MAX_N] = {0,};
		CNum D[MAX_N][4];
		int Max = 0;

		cin >> n >> m;

		if( n == 0 && m == 0 ){
			break;
		}

		for(int i = 0; i < n; i++){
			cin >> V[i];
		}

		sort(V, V + n);
		reverse(V, V + n);

		for(int i = 0; i < n; i++){
			for(int j = 0; j < 4; j++){
				if( j == 0 ){
					D[i][j].v.push_back( V[i] );
				}else if( j > 0 ){
					for(int k = 0; k <= i; k++){
						for(unsigned int l = 0; l < D[k][j - 1].v.size() > 0 ? 1 : 0; l++ ){
							int tmp = V[i] + D[k][j - 1].v[l];
							if( tmp <= m){
								if( !Find(D[i][j].v, tmp) ){
									D[i][j].v.push_back( tmp );
								}
							}
						}
					}
				}
			}
		}

		for(int i = 0; i < n; i++){
			//cout << i << '\n';
			for(int j = 0; j < 4; j++){
				//cout << " " << j << ":" << '\n';
				for(unsigned int k = 0; k < D[i][j].v.size(); k++ ){
					//cout << D[i][j].v[k] << ' ';
					int tmp = D[i][j].v[k];
					if( tmp <= m && Max < tmp){
						Max = tmp;
					}
				}
				//cout << '\n';
			}
		}
		cout << Max << '\n';
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;
int main(){
    while(true){
	ll n,m;
	cin >> n >> m;
	if(n == 0 && m == 0) break;
	n++;
	vector<ll> a(n);
	a[0] = 0;
	FOR(i,1,n){
	    cin >> a[i];
	}
	ll ans = 0;
	sort(ALL(a));
	FOR(i,0,n)FOR(j,i,n)FOR(k,j,n){
	    ll	p = m - (a[i]+a[j]+a[k]);
	    int h,mm,l;
	    h = n+1;
	    l = 0;
	    mm = (h+l)/2;
	    while(h-l>1){
		if(a[mm] > p) h = mm;
		else if(a[mm] < p)l = mm;
		else h = mm;
		mm = (h+l)/2;
	    }
	    if(a[i]+a[j]+a[k]+a[mm] <= m) ans = max(ans,a[i]+a[j]+a[k]+a[mm]);
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
	while(1){
	int n,m,p,q=0,r=0,l=0,s=0,e=0,ans=0;
	cin>>n>>m;
		if(n==0 && m==0)
			break;
	vector<int> a,x;
	a.push_back(0);
	for(int i=0;i<n;i++){
		cin>>p;
		a.push_back(p);
	}
	q=n+1;
	//cout<<q<<endl;
	for(int i=0;i<q;i++){
		for(int j=0;j<q;j++){
			x.push_back(a[i]+a[j]);
		}
	}
	sort(x.begin(),x.end());
	//for(int i=0;i<q*q;i++)
	//cout<<x[i]<<' ';
	//cout<<endl;
	for(int i=0;i<q*q;i++){
		r=m-x[i];
		//cout<<r<<"を探します"<<endl;
		s=0;
		e=q*q;
		while(1){
			l=(s+e)/2;
			//cout<<"l="<<l<<endl;
			if(x[l]>r){
				e=l-1;
			}else if(x[l]<r){
				s=l+1;
			}
			if(s>=e || x[l]==r){
				if(x[i]+x[l]<=m)
				ans=max(ans,x[i]+x[l]);
				break;
			}
		}
	}
		cout<<ans<<endl;
	}
	return 0;
}