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 <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(true){
		cin>>n>>m;
		if(n==0)	return 0;
		vector<int> p(n);
		for(int i=0;i<n;i++)	cin>>p[i];
		p.push_back(0);	n++;
		set<int> s;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++)	if(p[i]+p[j]<m)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(auto it=s.begin();it!=s.end();it++){
			auto it2=s.lower_bound(*it);
			if(it2!=s.begin())	it2--;
			if(*it+*it2<=m)	ans=max(ans,*it+*it2);
			else 	break;
		}
		cout<<ans<<endl;
	}
}

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

using namespace std;

int main()
{
	for(int n,m;cin>>n>>m,n|m;){
		vector<int> p(n);
		for(int i=0;i<n;i++)
			cin>>p[i];
		set<int> a;
		a.insert(0);
		for(int i=0;i<n;i++){
			a.insert(p[i]);
			for(int j=0;j<n;j++)
				a.insert(p[i]+p[j]);
		}
		int res=0;
		for(set<int>::iterator i=a.begin(),j;i!=a.end();++i){
			j=a.upper_bound(m-*i);
			if(j!=a.begin()){
				--j;
				res=max(res,*i+*j);
			}
		}
		cout<<res<<endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int n, m, arr[1001], sum[1000001];
	while (cin >> n >> m){
		if (!n && !m)return 0;
		int cnt = 0, res = 0;
		for (int i = 1; i <= n; ++i)scanf("%d", &arr[i]);
		for (int i = 0; i <= n; ++i){
			for (int j = n; j > i; --j){
				if (arr[i] + arr[j] <= m)sum[cnt++] = arr[i] + arr[j];
			}
		}
		sort(sum, sum + cnt);
		for (int i = 0; i < cnt; ++i){
			res = max(res, sum[i] + *(upper_bound(sum, sum + cnt, m - sum[i]) - 1));
		}
		printf("%d\n", res);
	}
}

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

typedef long long int ll;
using namespace std;

int main() {
	int N, M;
	while(1) {
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		vector<ll> P(N);
		vector<ll> Ps;
		Ps.push_back(0);
		for(int i = 0; i < N; i++) {
			cin >> P[i];
			Ps.push_back(P[i]);
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				Ps.push_back(P[i] + P[j]);
			}
		}
		sort(Ps.begin(), Ps.end());

		ll ans = 0;
		for (int i = 0; i < Ps.size(); i++) {
			if (Ps[i] > M) break;
			int k = std::upper_bound(Ps.begin(), Ps.end(), M - Ps[i]) - 1 - Ps.begin();
			ans = max(ans, Ps[i] + Ps[k]);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#define fi "input.txt"
#define fo "output.txt"
using namespace std;
long long n,m,a[100011],ans;
vector<long long > vt;
int main()
{
    freopen (fi , "r" ,stdin);
    freopen (fo , "w" ,stdout);
    scanf("%lld%lld",&n,&m);
    for (int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    n++;
    a[n]=0;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            vt.push_back(a[i]+a[j]);
        }
    sort(vt.begin(),vt.end());
    int c;
    c=vt.size()-1;
    for (int i=0;i<vt.size();i++)
    {
        while (c>=0&&vt[c]+vt[i]>m)
            c--;
        if (vt[c]+vt[i]<=m) ans=max(ans,vt[c]+vt[i]);
    }
    cout<<ans;
    return 0;
}

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

int p[1010], pp[1100000];

int main() {
    int n, m;
    while (1) {
        cin >> n >> m;
        if (n == 0 && m == 0) return 0;
        for (int i = 0; i < n; i++) cin >> p[i];

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

        int res = 0;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                if (p[i] + p[j] <= m) {
                    res = max(res, p[i] + p[j] + pp[upper_bound(pp, pp + (n + 1) * (n + 1), m - p[i] - p[j]) - pp - 1]);
                }
            }
        }

        cout << res << endl;
    }
}

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

constexpr int MAX_N = 1001;

int n, m;
int p[MAX_N];
std::vector<int> sums;

int main() {
	while (1) {
		std::cin >> n >> m;
		if (n + m == 0)
			break;
		sums.clear();
		for (int i = 0; i < n; ++i)
			std::cin >> p[i];
		++n;
		p[n - 1] = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = i; j < n; ++j)
				sums.push_back(p[i] + p[j]);
		}
		std::sort(sums.begin(), sums.end());
		sums.erase(std::unique(sums.begin(), sums.end()), sums.end());
		for (int i = 0; i < sums.size(); ++i)
			std::cout << sums[i] << ' ';
		std::cout << std::endl;
		int res = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				auto it = std::upper_bound(sums.begin(), sums.end(), m - p[i] - p[j]);
				if (it != sums.begin()) {
					--it;
					res = std::max(res, (*it) + p[i] + p[j]);
				}
			}
		}
		std::cout << res << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vi in(n+1);
		rep(i,n)cin>>in[i+1];
		vi cro;
		rep(i,n+1)rep(j,n+1)cro.pb(in[i]+in[j]);
		
		sort(all(cro));
		
		int out=0;
		rep(i,cro.size()){
			int d=m-cro[i];
			int h=cro.size();
			int l=0;
			while(1){
				if(h-l==1)break;
				int t=(h+l)/2;
				if(cro[t]>d)h=t;
				else l=t;
			}
			if(cro[i]+cro[l]<=m)out=max(out,cro[i]+cro[l]);
		}
		cout<<out<<endl;
	}
}

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

vector<long long>F1;
vector<long long>F2;

long long n,m;
long long a[10000];
long long maxn;
long long l,r,M;

int main(){
	while(true){
		maxn=0;
		cin>>n>>m;
		if(n==0 && m==0){break;}
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		F1.clear();
		F2.clear();
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				F1.push_back(a[i]+a[j]);
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				F2.push_back(a[i]+a[j]);
			}
		}
		sort(F2.begin(),F2.end());
		for(int i=0;i<F1.size();i++){
			if(F1[i]<=m){
				l=0;r=F2.size();
				for(int j=0;j<21;j++){
					M=(l+r)/2;
					if(F1[i]+F2[M]>m){
						r=M;
					}
					else if(M==F1.size()-1){
						l=M;
					}
					else if(F1[i]+F2[M+1]<=m){
						l=M;
					}
				}
				if(F1[i]+F2[M]<=m){
				maxn=max(maxn,F1[i]+F2[M]);
				}
			}
		}
		cout<<maxn<<endl;
	}
	return 0;
}