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 <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;
			map_sort(map, next, 4);
		}
		//cout << "- Answer -" << endl;
		cout << m - Min << endl;
	}
	return 0;
}

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

int n, m, point1[1001], sum, ans;
vector<int> point2;

int solve(int start, int end, int border){
    int mid;
    while(1){
        if((end + start) % 2){
            mid = (end + start) / 2 + 1;
        } else{
            mid = (end + start) / 2;
        }

        if(point2[mid] > border){
            end = mid;
        } else{
            start = mid;
        }

        if(end - start == 1){
            if(point2[end] < border){
                return point2[end];
            } else{
                return point2[start];
            }
        }
    }
}

int main(){
    while(cin >> n >> m && n && m){
        for(int i = 0; i < n; i++){
            cin >> point1[i];
        }
        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < m + 1; j++){
                point2.push_back(point1[i] + point1[j]);
            }
        }

        sort(point1, point1 + n + 1);
        sort(point2.begin(), point2.end());

        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < m + 1; j++){
                sum = point1[i] + point1[j];
                if(sum > m){
                    continue;
                }
                ans = max(ans, sum + solve(0, point2.size() - 1, m - sum));
            }
        }

        cout << ans << endl;

        memset(point1, 0, sizeof(point1));
        point2.clear();
        ans = 0;
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <cmath>
#include <limits>
#include <functional>

#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define FORR(i,m,n) for(int i=m;i>=n;i--)
#define pb(x) push_back(x) 
#define SORT(x) sort((x).begin(),(x).end())
#define INF 999999999

using namespace std;

#define int long long

signed main(){
	int n, m;
	cin >> n >> m;
	vector<int> p(n);
	REP(i, n){
		cin >> p[i];
	}
	p.push_back(0);
	sort(p.begin(), p.end());
	n++;
	
	vector<int> v;
	REP(i, n){
		REP(j, n){
			v.push_back(p[i]+p[j]);
		}
	}
	SORT(v);
	
	int l=0, r=v.size()-1, ans=0;
	REP(i, 10000000){
		int k=v[l]+v[r];
		if(k>m){
			if(r>0) r--;
		}
		else if(k<=m){
			ans=max(ans, k);
			if(l<v.size()-1) l++;
		}
	}
	
	cout << ans << endl;
}

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);
    while (1)
    {
    scanf("%lld%lld",&n,&m);
    if (n==0&&m==0) return 0;
    for (int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    ans=0;
    n++;
    a[n]=0;
    vt.clear();
    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<<endl;
    }
    return 0;
}