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 <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int n, m;
int main()
{
	while (cin >> n >> m, n)
	{
		vector<int> v, w;
		REP(i, n)
		{
			int a;
			cin >> a;
			w.push_back(a);
		}
		w.push_back(0);
		set<int> used;
		REP(i, w.size())REP(j, w.size())
		{
			int t = w[i] + w[j];
			if (used.find(t) == end(used))
			{
				used.insert(t);
				v.push_back(w[i] + w[j]);
			}
		}
		sort(ALL(v));
		int ans = 0;
		REP(i, v.size())
		{
			int tmp = m - v[i];
			if (tmp < 0) continue;
			int k = *(upper_bound(ALL(v), tmp) - 1);
			ans = max(ans, v[i] + k);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;

int p[1001];
int h[1000*1000+1];

int main(){
	int n,m,ans;
	while(cin>>n>>m){
		if(n==0&&m==0) break;
		p[0]=0;
		ans=0;
		for(int i=1; i<=n; ++i){
			cin >> p[i];
		}
		for(int i=0; i<=n; ++i){	
			for(int j=0; j<=n; ++j){
				h[i*n+j]=p[i]+p[j];
			}
		}
		sort(h,h+n*n+1);
		/*
		for(int i=0; i<=n*n; ++i){
			cout << h[i] << "\n";
		}
		*/
		for(int i=0; i<=n; ++i){	
			for(int j=0; j<=n; ++j){	
				int tmp=p[i]+p[j];
				int l=0,r=n*n;
				int cnt=0;
				while(1){
					int mid=(r+l)/2;
					if(cnt>=50){
						if(h[mid]+tmp<=m) ans=max(ans,h[mid]+tmp);
						break;
					}
					if(h[mid]+tmp>m){
						r=mid;
					} else if(h[mid]+tmp<m){
						l=mid;
					} else if(h[mid]+tmp==m){
						ans=m;
						break;
					}
					++cnt;
				}
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m;
int p[1001];
int pp[1500000];
int main() {
    while (scanf("%d%d",&n,&m)) {
        if (!n&&!m) break;
        memset(p, 0, sizeof(p));
        for (int i=0; i<n; i++) scanf("%d",&p[i]);
        sort(p, p+(++n));
        for (int c=0; c<=n; c++) {
            for (int d=0; d<=n; d++) {
                pp[c*n+d]=p[c]+p[d];
            }
        }
        sort(pp, pp+n*n);
        int res=0;
        for (int i=0; i<n*n; i++) {
            int temp = upper_bound(pp,pp+n*n,m-pp[i])-pp-1;
            if (pp[temp]+pp[i]<=m) res = max(res, pp[temp]+pp[i]);
        }
        printf("%d\n",res);
    }
}

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

int N,M,P[1010];
int dp[1010][5] = {};

int main(){
	while(cin >> N >> M && N>0){
		for(int i=1;i<=N;i++){
			cin >> P[i];
		}
		vector<int> v;
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				v.push_back(P[i]+P[j]);
			}
		}
		sort(v.begin(),v.end());
		int ans = 0;
		for(int i=0;i<v.size();i++){
			if(M-v[i]<0) continue;
			int s = upper_bound(v.begin(),v.end(),M-v[i])-v.begin();
			s--;
			ans = max(ans,v[i]+v[s]);
		}
		cout << ans << endl;
	}
}

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

#define MAX_N 1001

using namespace std;

void Uniq(vector<int> &v){
	for(unsigned int i = 0; i < v.size(); i++){
		int sc = v[i];
		for(unsigned int j = i + 1; j < v.size(); j++ ){
			if(sc == v[j]){
				v.erase( v.begin() + j );
				j--;
			}else if(sc < v[i]){
				break;
			}
		}
	}
}

int BinSearch(vector<int> &v, int search){
	int l = 0;
	int r = v.size();
	int m = 0;
	while( r - 1 > l ){
		m = (r + l) / 2;

		if( v[m] > search ){
			r = m;
		}else if(v[m] == search){
			return m;
		}else{
			l = m;
		}
	}
	return m;
}

int main(void){
	while( true ){
		int index = 0;
		int Max = 0;
		int N, M;
		vector<int> Q;
		vector<int> V;

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

		for(int i = 0; i < N; i++){
			int tmp;
			cin >> tmp;
			V.push_back( tmp );
		}
		V.push_back( 0 );
		Uniq( V );

		for(unsigned int i = 0; i < V.size(); i++){
			for(unsigned int j = 0; j < V.size(); j++){
				Q.push_back(V[i] + V[j]);
			}
		}
		//cout << "Sorting..." << endl;
		sort(Q.begin(), Q.end());

		//cout << "Uniqing..." << endl;
		Uniq( Q );

		for(unsigned int i = 0; i < Q.size(); i++){
			//cout << Q[i];
			int optim = BinSearch(Q, M - Q[i]);
			//cout << ' ' << optim << ":" << Q[optim] << '\n';
			if( optim > 0 && Q[i] + Q[optim] > M) optim--;
			Max = max( Max , Q[i] + Q[optim] > M ? 0 : Q[i] + Q[optim]);
		}

		cout << Max << '\n';
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a,b) (abs((a)-(b))<EPS)
int p[1001];
int ans;
vector<int> vec;
int n,m;
int main()
{
	while(1)
	{
		memset(p,0,sizeof(p));
		vec.clear();
		cin >> n >> m;
		ans=0;
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++)
		{
			cin >> p[i];
		}
		for(int i=0;i<n;i++)
		{
			vec.pb(p[i]);
			for(int j=0;j<n;j++)
			{
				vec.pb(p[i]+p[j]);
			}
		}
		sort(vec.begin(),vec.end());
		for(int i=0;i<sz(vec);i++)
		{
			vector<int>::iterator it=upper_bound(vec.begin(),vec.end(),m-vec[i]);
			if(it==vec.begin())continue;
			it--;
			ans=max(*it+vec[i],ans);
		}
		cout << ans << endl;
	}
	return 0;
}