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>
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>

using namespace std;


void func(int N,int M){
    vector<int> First,Second;

    First.push_back(0);
    int tmp,max;

    for(int i = 0; i < N; i++){
        scanf("%d",&tmp);
      	First.push_back(tmp);
    }

    sort(First.begin(),First.end());

    for(int i = 0; i <= N;i++){
    	for(int k = i; k <= N;k++){
    		if(First[i]+First[k] <= M){
    			Second.push_back(First[i]+First[k]);
    		}else{
    			break;
    		}
    	}
    }

    sort(Second.begin(),Second.end());

    int limit = Second.size()-1,div,left,right,max_min,m;

    max = 0;
    for(int i = limit; i >= 0;i--){
    	div = M - Second[i];
    	max_min = 0;
    	left = 0,right = limit;
    	m = (left+right)/2;
    	while(left <= right){
    		if(Second[m] > div){
    			right = m-1;
    		}else{
    			max_min = (max_min >= Second[m])?max_min:Second[m];
    			left = m + 1;
    		}
    		m = (left+right)/2;
    	}
    	max = (max >= Second[i]+max_min)?max:Second[i]+max_min;
    }

    printf("%d\n",max);

}

int main(){

    int N,M;

    while(true){
        scanf("%d %d",&N,&M);
        if(N == 0 && M == 0)break;

        func(N,M);
    }

    return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

int N, M, arr[1000];

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    while (true) {
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        arr[0] = 0; ++N;
        for (int j = 1; j < N; ++j) {
            cin >> arr[j];
        }
        vector<int> arr2;
        for (int j = 0; j < N; ++j) for (int k = 0; k < N; ++k) if (arr[j] + arr[k] <= M) {
            arr2.push_back(arr[j] + arr[k]);
        }
        sort(begin(arr2), end(arr2));
        const int NN = arr2.size();

        int ans = 0, r = NN-1;
        for (int j = 0; j < NN; ++j) {
            while (arr2[j] + arr2[r] > M) --r;
            if (arr2[j] + arr2[r] <= M) {
                ans = max(ans, arr2[j] + arr2[r]);
            }
        }

        cout << ans << endl;
    }
    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){
	int 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];
	}
	int ans = 0;
	vector<int> v;
	REP(i,n)FOR(j,1,n){
	    if(a[i]+a[j] <= m)
		v.PB(a[i]+a[j]);
	}
	sort(ALL(v));
	v.erase(unique(ALL(v)),v.end());
	REP(i,v.size()){
	    int temp = m - v[i];
	    int h,mm,l;
	    h = v.size()+1;
	    l = 0;
	    mm = (h+l)/2;
	    while(h-l>1){
		if(v[mm] > temp) h = mm;
		else if(v[mm] < temp) l = mm;
		else break;
		mm = (h+l)/2;
	    }

	    if(v[mm] + v[i] <= m) ans = max(ans,v[mm]+v[i]);
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>

static const int MOD = 1000000007;
using ll = int64_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int n, m;
    while(cin >> n >> m, n|m){
        vector<int> v(n+1), u((n+1)*(n+1));
        for (int i = 0; i < n; ++i) {
            scanf("%d", &v[i+1]);
        }
        for (int i = 0; i <= n; ++i) {
            for (int j = 0; j <= n; ++j) {
                u[i*n+j] = v[i]+v[j];
            }
        }
        sort(u.begin(), u.end());
        u.erase(unique(u.begin(), u.end()), u.end());
        int ans = 0;
        for (auto &&i : u) {
            if(i <= m) chmax(ans, i+*(upper_bound(u.begin(), u.end(), m-i)-1));
        }
        cout << ans << "\n";
    }
    return 0;
}

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

set<long long> sep,sem;
long long ary[1010];
int main(){
	int N;
	long long M,hog,res;
	set<long long>::iterator it,it2;
	while(1){
		sep.clear();
		sem.clear();
		cin>>N>>M;
		if(N==0&&M==0) break;
		for(int i=0;i<N;i++){
			cin>>ary[i];
		}
		ary[N]=0;
		for(int i=0;i<=N;i++){
			for(int j=i;j<=N;j++){
				sep.insert(ary[i]+ary[j]);
				sem.insert(-ary[i]-ary[j]);
			}
		}
		res=0;
		for(it=sep.begin();it!=sep.end();it++){
			if((*it)>M)break;
			hog=(*it)-M;
			it2=lower_bound(sem.begin(),sem.end(),hog);
			res=max(res,(*it)-(*it2));
		}
		cout<<res<<endl;

	}
	return 0;
}

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

using namespace std;

int main()
{
	int N, M, D;

	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

		if (N == 0 && M == 0) { break; }

		vector<int> C(N + 1); C.push_back(0);

		for (int i = 1; i <= N; i++)
		{
			scanf("%d", &C[i]);
		}

		vector<int> r;

		for (int i = 0; i <= N; i++)
		{
			for (int j = i; j <= N; j++)
			{
				if (C[i] + C[j] <= M)
				{
					r.push_back(C[i] + C[j]);
				}
			}
		}

		sort(r.begin(), r.end());

		int ret = 0, lc = 0, rc = r.size() - 1;

		while (rc - lc >= 0)
		{
			if (r[lc] + r[rc] > M)
			{
				rc--;
			}
			else
			{
				ret = max(ret, r[lc++] + r[rc]);
			}
		}

		printf("%d\n", ret);
	}

	return 0;
}