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 <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef vector<int> VI;

int binary_search(int tmp_m, VI &second, int i) {
    int left = 0;
    int right = second.size() - 1;
    int mid = (left + right) / 2 + 1;
    while(1) {
        if (right - left <= 1) {
            return second[left] + i;
        }
        mid = (left + right) / 2;
        if (second[mid] + i <= 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;

        table[0] = 0;
        for (int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            if (temp <= m) {
                table.push_back(temp);
            }
        }

        for (int i = 0; i < (int)table.size(); i++) {
            for (int j = i; j < (int)table.size(); j++) {
                int sum = table[i] + table[j];
                if (sum <= m) {
                    first.push_back(table[i] + table[j]);
                }
            }
        }
        sort(first.begin(), first.end());

        int ans = 0;
        for (int i = 0; i < (int)(first.size()); i++) {
            int total = binary_search(m, first, first[i]);
            if (total <= m && ans < total) {
                ans = total;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

LL N, M, P[1123];
std::set<LL>list;
LL ans;

int main()
{
	while (true)
	{
		N = M = 0;
		memset(P, 0, sizeof(P));
		ans = 0;

		in >> N >> M;
		if (N == 0 && M == 0) { break; }
		rep(i, N) { in >> P[i]; }

		P[N] = 0;
		N++;
		std::sort(P, P + N);
		rep(i, N)
		{
			rep(j, N) { list.insert(P[i] + P[j]); }
		}
		for (auto i : list)
		{
			if (i > M) { continue; }
			auto iter = std::upper_bound(list.begin(), list.end(), M - i);
			--iter;
			ans = std::max(ans, i + *iter);
		}

		out << ans << std::endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int max_p[200000001];
int p[1001];
int main ()
{
	long n,m;
	long i,j;
	int r;
	while(cin >> n >> m,n)
	{
		for(i=0;i<=m;i++)
		{
			max_p[i]=0;
		}
		for(i=0;i<n;i++)
		{
			cin >> p[i];
		}
		max_p[0]=1;
		for(i=0;i<m;i++)
		{
			if(max_p[i]>=1 && max_p[i]<5)
			{
				r=max_p[i];
				for(j=0;j<n;j++)
				{
					if(i+p[j]<=m)
					{
						if(max_p[i+p[j]]==0 || r+1<max_p[i+p[j]])
						{
							max_p[i+p[j]]=r+1;
						}
					}
				}
			}
		}
		for(i=m;i>=0;i--)
		{
			if(max_p[i]!=0)
			{
				cout <<  i << endl;
				break;
			}
		}
	}
	return 0;
}

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector<long long int> data;
		long long int b;
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				data.push_back(b);
			}
		}
		sort(data.begin(), data.end());
		vector<long long int> data2;
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		for (long long int i = 0; i < data2.size(); i++) {
			long long int a = data2[i];
			data.push_back(a);
			for (long long int j = i+1; j < data2.size(); j++) {
				if (data2[j] == a) {
					i++;
				} else {
					break;
				}
			}
		}
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		long long int sz = data2.size();
		cout << data2[sz-1] << endl;
	}
	return 0;
}