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 <cstdio>
#include <algorithm>

using namespace std;


int a[1000*1001];

int main(){
	int n,m;
	int i,j,k;
	int *p;
	int r,rr;

	while(true){

		//memset(a,0,sizeof(a));
		scanf("%d%d",&n,&m);
		if(n==0&&m==0) break;

		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
		}

		k=n;
		for(i=0;i<n;i++){
			for(j=i;j<n;j++){
				a[k++]=a[i]+a[j];
				}
		}
		sort(a,a+n*(n+1)/2+n);

	/*
		for(i=0;i<n*(n+1)/2+n;i++){
			printf("%d\n",a[i]);
		}
		*/
		rr=0;
		for(i=0;i<n*(n+1)/2+n;i++){
			p=lower_bound(a+i,a+n*(n+1)/2+n,m-a[i]);
			r=*(p-1)+a[i];
			if((*(p-1)<=m-a[i])&&(r>rr)) rr=r;
			}

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

		}

	return 0;
	}

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

using namespace std;


int P[1001];
int twice[1002010];
vector<int> vi;


int main(void){
	int N,M;
	
	while(scanf("%d%d", &N, &M) && N != 0 && M != 0 ){
		int ans = 0;
		
		P[0] = 0;
		
		for(int i = 1; i <= N; i++){
			scanf("%d", &P[i]);
		}
		
		for(int i = 0; i <= N; i++){
			for(int j = 0; j <= N; j++){
				vi.push_back( P[i]+P[j] );
			}
		}
		
		sort(vi.begin(),vi.end());
		
		twice[0] = vi[0];
		int c = 1;
		
		for(int i = 1; i < vi.size(); i++){
			if(vi[i] != vi[i-1]){
				twice[c] = vi[i];
				c++;
			}
		}
		/*
		for(int i = 0; i < c; i++){
			printf("%d\n",twice[i]);
		}
		*/
		//cツ古つづ荊wice
		
		for(int i = 0; i < c; i++){
			int *p = upper_bound(twice,twice+c,M-twice[i]);
			int u = p - twice;
			int t = u-1;
			if(twice[i]+twice[u]<=M)ans = max(ans,twice[i]+twice[u]);
			if(t != -1 &&twice[i]+twice[t]<=M)ans = max(ans,twice[i]+twice[t]);
		}
		
		printf("%d\n",ans);
		vi.clear();
		
	}
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <queue>

using namespace std;

struct data {
	int num;
	char count;
};

int main( void )
{
	int n, m, input[1000];
	while(cin >> n >> m, n | m) {
		for(int i=0; i < n; ++i) cin >> input[i];
		queue<data> q; data a;
		a.num = m; a.count = 0;
		q.push(a);
		sort(input, input+n, greater<int>());
		//for(int i=0; i < n; ++i) cout << input[i] << ' ';
		//cout << endl;
		int Min = m;
		while(!q.empty()) {
			data tmp = q.front(); q.pop();
			for(int i=0; i < n; ++i) {
				int bb = tmp.num - input[i];
				if(bb >= 0 && tmp.count < 4) {
					Min = min(Min, bb);
					data x = { bb, tmp.count + 1 };
					q.push(x);
					if(!Min) break;
				}
			}
			if(!Min) break;
		}
		cout << m - Min << endl;
	}
	return 0;
}

C++: #import<iostream>
#import<algorithm>
using namespace std;int*p,P[1001],*q,Q[1<<20],n,m,v,*i,*j;main(){for(;cin>>n>>m,n;cout<<v<<endl){for(p=P+1;n--;v=0)cin>>*p++;sort(P,p);p=unique(i=P,p);for(q=Q;i-p;++i)for(j=P;j<i+1;)*q++=*j+++*i;sort(Q,q);q=unique(i=Q,q);for(*q++=m+1;*i<=m;++i)v=max(v,*i+upper_bound(Q+1,q,m-*i)[-1]);}}

C++: #include<iostream>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<cstdlib>
#include<typeinfo>
#include<sstream>
#include<list>
#include<set>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define fori(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
int in()
{
	int x;cin>>x;
	return x;
}
int stringInt(string a)
{
	return atoi(a.c_str());
}
string intString(int a)
{
	stringstream ss;
	ss<<a;
	return ss.str();
}
vector<int> SieveOfEratosthenes(int x)
{
	vector<int> a;
	vector<int> b(x+1,0);
	for(int i=2;i<=x;i++)
	{
		if(b[i]==0)
		{
			a.push_back(i);
			for(int l=i;l<=x;l+=i)
			{
				b[l]=1;
			}
		}
	}
	return a;
}

int main ()
{
	int n,m;
	while(cin>>n>>m&&n&&m)
	{
		vector<int> a,b,c;
		b.push_back(0);
		rep(i,n)
		{
			b.push_back(in());
		}
		rep(i,n+1)
		{
			rep(l,n+1)
			{
				a.push_back(b[i]+b[l]);
			}
		}
		rep(i,a.size())
		{
			rep(l,a.size())
			{
				c.push_back(a[i]+a[l]);
			}
		}
		sort(c.begin(),c.end());
		cout<<*(upper_bound(c.begin(),c.end(),m)-1)<<endl;
	}

}