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*len(arr)+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 <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#define fi "darts.inp"
#define fo "darts.out"
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 <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

int main() {
	int n, m;
	while (cin >> n >> m, n){
		vector<int> point(n);
		REP(i, n){
			cin >> point[i];
		}
		point.push_back(0);
		vector<int> sum = point;
		REP(i, n + 1){
			REP(j, n + 1){
					sum.push_back(sum[i] + sum[j]);
			}
		}
		SORT(sum);
		int ma = 0;
		REP(i, sum.size()){
			int left = 0;
			int right = sum.size() - 1;
			while (left < right){
				if (sum[left + (right - left) / 2] + sum[i] <= m){
					ma = max(sum[left + (right - left) / 2] + sum[i], ma);
					left = left + (right - left) / 2 + 1;

				}
				else{
					right = left + (right - left) / 2 - 1;
				}
			}
			
		}
		cout << ma << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <cstring>
#include <cstdlib>

using namespace std;

int array_sum( int ary[4][1001], int idx, int size )
{
	int sum = 0;
	for(int i=0; i < size; ++i) sum += ary[i][idx];
	return sum;
}
void map_sort( int ary[4][1001], int idx, int size )
{
	for(int i=0; i < size; ++i)
		for(int j=i+1; j < size; ++j) {
			if(ary[i][idx] < ary[j][idx])
				swap(ary[i][idx], ary[j][idx]);
		}
}

int main( void )
{
	int n=0, m, input[1001], map[4][1001];
	while(cin >> n >> m, n | m) {
		for(int i=0; i < n; ++i) {
			cin >> input[i];
			for(int j=0; j < 4; ++j) map[j][i] = 0;
		}
		for(int i=0; i < 4; ++i) map[i][n] = 0;
		sort(input, input+n, greater<int>());
		int sum = 0, count = 0;
		while(sum < m) {
			sum += input[0];
			++count;
		}
		int mmm = m, cc = 0;
		for(int i=0; i < n; ++i) {
			while(input[i] <= mmm && cc < 4) {
				mmm -= input[i];
				++cc;
			}
			if(cc >= 4) break;
		}
		sort(input+1, input+n);
		for(int i=0; i < count; ++i) map[i][0] = input[0];
		int sub = sum - m;
		if(!sub) { cout << m << endl; continue; }
		else if(sub < 0) { cout << sum << endl; continue; }
		int Min = min(m, mmm);
		//cout << Min << endl;
		input[n] = 0;
		for(int i=0; i < n; ++i) {
			//cout << input[i] << endl;
			sum = array_sum(map, i, count);
			if(sum == m) { Min = 0; break; }
			int next = i + 1, ii = i + 1;
			for(int j=0; j < count; ++j) {
				int tsum = sum - map[j][i] + input[ii];
				int tsub = tsum - m;
				if(tsub > 0) {
					if(sum > tsum) {
						sum = tsum;
						map[j][next] = input[ii];
					}
					else
						map[j][next] = map[j][i];
				}
				else if(!tsub) {
					Min = 0; break;
				}
				else { // minus
					Min = min(Min, -tsub);
					int tcount = 4 - count;
					int tmul = tcount * input[ii];
					if(tmul >= -tsub) {
						for(int k=0; k <= j; ++k) map[k][next] = input[ii];
						for(int k=j+1; k < count; ++k) map[k][next] = map[k][i];
						for(int k=count; k < 4; ++k) {
							if(sum < array_sum(map, next, k) + input[ii]) break;
							map[k][next] = input[ii];
							++count;
						}
						//input[ii] = 0;
						//i -= 2;
						break;
					}
					else {
						map[j][next] = map[j][i];
					}
				}
			}
			if(!Min) break;
			map_sort(map, next, 4);
			//cout << "--------------------------------------------" << endl;
			//for(int y=0; y < 4; ++y, cout << endl)
			//	for(int x=0; x < n+1; ++x) cout << map[y][x] << ' ';
			//cout << "--------------------------------------------" << endl;
		}
		if(!Min) { cout << m << endl; continue; }
		for(int i=0; i < count; ++i) map[i][0] = input[0];
		for(int i=0; i < n; ++i) {
			sum = array_sum(map, i, count);
			if(sum == m) { Min = 0; break; }
			int next = i + 1, ii = i + 1;
			for(int j=0; j < count; ++j) {
				int tsum = sum - map[j][i] + input[ii];
				int tsub = tsum - m;
				if(tsub > 0) {
					if(sum > tsum) {
						sum = tsum;
						map[j][next] = input[ii];
					}
					else
						map[j][next] = map[j][i];
				}
				else if(!tsub) {
					Min = 0; break;
				}
				else { // minus
					Min = min(Min, -tsub);
					map[j][next] = map[j][i];
				}
			}
			if(!Min) break;
		}
		//cout << "- Answer -" << endl;
		cout << m - Min << endl;
	}
	return 0;
}

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

pair<int,pair<int,int>> P(int a,int b,int c){
	pair<int,pair<int,int>> ret;
	ret.first=a; ret.second.first=b; ret.second.second=c;
	return ret;
}

int main(){
while(1){
	int n,m,a,ans=0;
	vector<int>p,pp;
	scanf("%d%d",&n,&m);
	if(n==0&&m==0)break;
	p.push_back(0);
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		p.push_back(a);
	}
	sort(p.begin(),p.end());
	priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;
	pq.push(P(0,0,0));
	while(!pq.empty()){
		pair<int,pair<int,int>> pai=pq.top(); pq.pop();
		if(pai.first<=m){
			pp.push_back(pai.first);
			pq.push(P(p[pai.second.first]+p[pai.second.second+1],pai.second.first,pai.second.second+1));
			if(pai.second.second==0)pq.push(P(p[pai.second.first+1]+p[pai.second.second],pai.second.first+1,pai.second.second));
		}
	}
	int s=pp.size()-1;
	for(int i=0;i<=s;i++){
		while(pp[i]+pp[s]>m)s--;
		if(ans<pp[i]+pp[s])ans=pp[i]+pp[s];
	}
	printf("%d\n",ans);
}
}