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>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional> //sort(A,A.end(),greater<int>());???????????????
#include <queue>
#include <stack>
#include <vector>

#define INF 10 << 10

long p[1001];
long dp[4][1001];

int main() {
	int n;
	long m;
	while (cin >> n >> m && n != 0 && m != 0) {
		for (int i = 1; i <= n; ++i) {
			cin >> p[i];
		}
		for (int i = 0; i < 4; ++i) { dp[i][0] = 0; }
		for (int i = 0; i < 4; ++i) {
			for (int i2 = 1; i2 <= n; ++i2) {
				if (i == 0) {
					if (p[i2] <= m) {
						dp[i][i2] = max(dp[i][i2 - 1], p[i2]);
					}
					else { dp[i][i2] = dp[i][i2 - 1]; }
				}
				else {
					for (int i3 = 0; i3 <= n; ++i3) {
						if (dp[i - 1][i3] + p[i2] <= m) {
							dp[i][i2] = max(dp[i][i2], dp[i - 1][i3] + p[i2]);
						}
					}
				}
			}
		}

		cout << dp[3][n] << endl;
	}
}

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

const LL inf = 112345678901;
LL N, M, P[1123];
LL memo[1123][5];

LL dp(LL p, LL c)
{
	LL res = 0;
	if (p == -1) { return 0; }
	if (c > 4) { return memo[p][c] = 0; }
	if (memo[p][c] != -1) { return memo[p][c]; }
	if (dp(p - 1, c + 1) + P[p] <= M) { res = dp(p - 1, c + 1) + P[p]; }
	if (dp(p, c + 1) + P[p] <= M) { res = std::max(res, dp(p, c + 1) + P[p]); }
	if (dp(p - 1, c) <= M) { res = std::max(res, dp(p - 1, c)); }
	return memo[p][c] = res;
}

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

		in >> N >> M;
		if (N == 0 && M == 0) { break; }
		rep(i, N) { in >> P[i]; }
		std::sort(P, P + N);
		out << dp(N - 1, 0) << std::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]); lc++;
			}
		}

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

	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, p[1010];
vector<int> vi;

int main(){
	while (cin >> n >> m&&n + m){
		vi.clear();
		p[0] = 0;
		FOR(i, 1, n + 1)cin >> p[i];
		REP(i, n + 1){
			REP(j, n + 1){
				if (p[i] + p[j] <= m){
					vi.push_back(p[i] + p[j]);
				}
			}
		}
		int ans = -1;
		sort(ALL(vi));
		vi.erase(unique(ALL(vi)), vi.end());
		REP(i, vi.size()){
			REP(j, vi.size()){
				if (vi[i] + vi[j] <= m){
					ans = max(ans, vi[i] + vi[j]);
				}
			}
		}
		cout << ans << endl;
	}
}

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

using namespace std;

int main()
{
	int n, m, a[1001], t, p, q, r;
	bool f;
	
	while(cin>>n>>m&&n&&m){
		vector<int> vec;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
		}
		a[0] = 0;
		
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				r = a[i]+a[j];
				f = true;
				for(int k = 0; k < vec.size(); k++){
					if(r == vec[i]){
						f = false;
						break;
					}
				}
				if(f){
					vec.push_back(r);
				}
			}
		}
		sort(vec.begin(), vec.end());
		
		t = 0;
		for(int i = 0; i < vec.size(); i++){
			p = 0;
			q = vec.size();
			while(q-p > 1){
				r = (p+q)/2;
				if(vec[i]+vec[r] >= m){
					q = r;
				} else {
					p = r;
				}
			}
			r = vec[i]+vec[p];
			if(t < r && r <= m){
				t = r;
			}
		}
		
		cout << t << endl;
	}
	
	return 0;
}