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

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

using namespace std;

int main() {
	int n, m;
	while (cin >> n >> m, n + m) {
		vector<int> p(n + 1), tmp;
		for (int i = 0; i < n; ++i) cin >> p[i];
		p[n] = 0;
		for (int i = 0; i <= n; ++i)
			for (int j = i; j <= n; ++j)
				if (p[i] + p[j] <= m) tmp.push_back(p[i] + p[j]);
		sort(tmp.begin(), tmp.end());
		int ans = 0;
		for (int i = 0; i < tmp.size(); ++i)
			ans = max(ans, tmp[i] + *(upper_bound(tmp.begin(), tmp.end(), m - tmp[i]) - 1));
		cout << ans << endl;
	}
		
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
typedef long long L;
using namespace  std;
int main(){
	int n;
	L m,p[1001],sum=0;
	vector<L> sum1;
	vector<L>::iterator it,ele;
	scanf("%d %lld",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%lld",&p[i]);
	}
	p[n]=0;
	for(int i=0;i<=n;i++){
		for(int i2=0;i2<=n;i2++){
			sum1.push_back(p[i]+p[i2]);
		}
	}
	sort(sum1.begin(),sum1.end());
	for(it=sum1.begin();it<sum1.end();it++){
		if(*it>m)
			break;
		ele=upper_bound(sum1.begin(),sum1.end(),(m-(*it) ) );
		if(ele!=sum1.begin()){
			ele--;
		}
		if(sum<=(*it)+(*ele)){
			sum=(*it)+(*ele);
		}
		if(sum==m)
			break;
	}
	printf("%lld\n",sum);
	return 0;
}

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

vector<long long> sep;

long long ary[1010];
int main(){
	int N;
	long long M,res;
	vector<long long>::iterator it,it2;
	while(1){
		sep.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++){
				if(ary[i]+ary[j]<=M){
					sep.push_back(ary[i]+ary[j]);
				}
			}
		}
		sort(sep.begin(),sep.end());
		res=0;
		it2=sep.end();
		it2--;
		for(it=sep.begin();it!=sep.end();it++){
			while((*it)+(*it2)>M)it2--;
			if((*it)>(*it2)) break;
			res=max(res,(*it)+(*it2));
		}
		cout<<res<<endl;

	}
	return 0;
}