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<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
long long a[1001];
long long b[1000000];
int main() {
	int n, m;
	while (cin >> n >> m&&n != 0 && m != 0) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		a[n] = 0;
		int t = 0;
		long long sum = 0;
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				b[i*n + j] = a[i] + a[j];
				t++;
			}
		}
		for (int i = 0; i < t; i++) {
			for (int j = 0; j < t; j++) {
				if (b[i] + b[j] <= m) sum = max(sum, b[i] + b[j]);
			}
		}
		cout << sum << endl;
	}
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

int binary_search(int tmp_m, VI second) {
    if (tmp_m <= 0) {
        return 0;
    }
    int left = 0;
    int right = second.size() - 1;
    int mid = (left + right) / 2 + 1;
    while(1) {
        if (right - left <= 1) {
            return second[left];
        }
        mid = (left + right) / 2;
        if (second[mid] <= tmp_m) {
            left = mid;
        } else {
            right = mid;
        }
    }
    return 0;
}

int main(void) {
    while (1) {
        int n;
        int m;
        cin >> n >> m;
        if (n == 0 && m == 0) {
            break;
        }
        VI table(n + 1);
        VI first((n + 1) * (n + 1));
        VI second((n + 1) * (n + 1));

        table[0] = 0;
        for (int i = 0; i < n; i++) {
            cin >> table[i + 1];
        }
        sort(table.begin(), table.end());

        for (int i = 0; i < n + 1; i++) {
            for (int j = 0; j < n + 1; j++) {
                first[i * (n + 1) + j] = table[i] + table[j];
                second[i * (n + 1) + j] = table[i] + table[j];
            }
        }

        sort(first.begin(), first.end());
        sort(second.begin(), second.end());

        int ans = 0;
        for (int i = 0; i < (int)(first.size()); i++) {
            int total = first[i] + binary_search(m - first[i], second);
            /*
            cout << "START" << endl;
            cout << first[i] << " " << total << endl;
            cout << "TOTAL = " <<  total << endl;
            */
            if (total <= m) {
                ans = max(ans, total);
            }
            //cout << "END" << endl << endl;
        }
        //cout << "ANS = " <<  ans << endl;
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (true) {
        ll N, M;
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        vector<ll> P(N + 1);
        P.at(0) = 0;
        for (int i = 1; i <= N; ++i) {
            cin >> P.at(i);
        }
        vector<ll> V;
        for (int i = 0; i <= N; ++i) {
            for (int j = 0; j <= N; ++j) {
                V.push_back(P.at(i) + P.at(j));
            }
        }
        sort(V.begin(), V.end());

        ll ans = 0;
        for (int i = 0; i < V.size(); ++i) {
            auto itr = upper_bound(V.begin(), V.end(), M - V.at(i));
            if (itr == V.begin()) continue;
            ans = max(ans, V.at(i) + *(itr - 1));
        }
        cout << ans << endl;
    }

    return 0;
}

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

int main()
{
	int n,m,in,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&in);
			P.push_back(in);
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			if(P[i] + *it >= m){
				MAX = max(MAX,P[i] + *it);
			}
		}
	
		printf("%d\n",MAX);
	}
	
	return 0;
}

C++: #include <iostream>
#include <set>

using namespace std;

int main()
{
	int n, m, a[1001], s, t;
	
	while(cin>>n>>m&&n&&m){
		set<int> st;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
		}
		a[0] = 0;
		t = 0;
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				st.insert(a[i]+a[j]);
			}
		}
		
		bool f = true;
		for(set<int>::iterator it1 = st.begin(); it1 != st.end() && f; it1++){
			for(set<int>::iterator it2 = st.begin(); it2 != st.end(); it2++){
				s = (*it1) + (*it2);
				if(t <  s&& s <= m){
					t = s;
					if(t == m){
						f = false;
						break;
					}
				}
			}
		}
		
		cout << t << endl;
	}
	
	return 0;
}

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

using namespace std;

int main()
{
	int N, M, D;

	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

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

		vector<int> C; C.push_back(0);

		for (int i = 0; i < N; i++)
		{
			scanf("%d", &D);

			if (D <= M)
			{
				C.push_back(D);
			}
		}

		vector<int> r;

		for (int i = 0; i < C.size(); i++)
		{
			for (int j = i; j < C.size(); j++)
			{
				if (C[i] + C[j] <= M)
				{
					r.push_back(C[i] + C[j]);
				}
			}
		}

		sort(r.begin(), r.end());

		int ret = 0, lc = 0, rc = r.size() - 1;

		while (rc - lc >= 0)
		{
			if (r[lc] + r[rc] > M)
			{
				rc--;
			}
			else
			{
				ret = max(ret, r[lc++] + r[rc]);
			}
		}

		printf("%d\n", ret);
	}

	return 0;
}