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 <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
# define ALL(x)     (x).begin(),(x).end()
# define UNIQ(c)    (c).erase(unique(ALL((c))), end((c)))
# define mp         make_pair
# define eb         emplace_back
# define FOR(i,a,b) for(int i=(a);i<(b);++i)
# define REP(i,n)   FOR(i,0,n)
# define INIT       std::ios::sync_with_stdio(false);std::cin.tie(0);

int p[1001];

int main() {
	int n, m;
	while (cin >> n >> m&&n&&m) {
		REP(i, n)cin >> p[i];
		vector<int> vec;
		REP(i, n + 1)REP(j, n + 1) {
			vec.emplace_back(p[i] + p[j]);
		}
		sort(vec.begin(), vec.end());
		int ans = 0;
		REP(i, vec.size()) {
			int num = m - vec[i];
			if (num < 0)continue;
			int num2 = upper_bound(vec.begin(), vec.end(), num) - 1 - vec.begin();
			ans = max(vec[i] + vec[num2], ans);
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



int N, M;
vint vec;
vint half;

int main() {
    while (cin >> N >> M) {
        if (N == 0) break;
        half.clear(); vec.clear();
        half.PB(0);
        for (int i = 0; i < N; ++i) {int a; cin >> a; vec.PB(a); half.PB(a);}
        for (int i = 0; i < N; ++i) for (int j = i; j < N; ++j) half.PB(vec[i] + vec[j]);
        sort(ALL(half));
        half.erase(unique(ALL(half)), half.end());
        
        //COUT(half);
        
        int res = 0;
        for (int i = 0; i < half.size(); ++i) {
            int h = half[i];
            int rem = M-h;
            
            int id = upper_bound(ALL(half), rem) - half.begin();
            --id;
            if (id < 0) continue;
            
            chmax(res, h + half[id]);
        }
         
        cout << res << endl;
    }
    return 0;
}

C++: a

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

#define pb push_back

int main()
{
	int N, M;
	while (cin >> N >> M, N||M)
	{
		vector<int> in;
		in.pb(0);
		for (int i=0; i<N; i++)
		{
			int t;
			cin >> t;
			in.pb(t);
		}

		vector<int> in2;
		for (int i=0; i<=N; i++)
		for (int k=0; k<=N; k++)
		{
			in2.pb(in[i]+in[k]);
		}
		sort(in2.begin(), in2.end());
		in2.erase(unique(in2.begin(), in2.end()), in2.end());

		int ans = 0;
		for (int i=0; i<in2.size(); i++)
		{
			if (in2[i] > M) break;

			vector<int>::iterator it = upper_bound(in2.begin(), in2.end(), M-in2[i]);
			if(it != in2.begin()) {it--;}
			
			ans = max(ans, in2[i] + *it);
		}

		cout << ans << endl;
	}

	return 0;
}

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

int main(){
while(1){
	int n,m,pi;
	vector<int> x,p;

	scanf("%d%d",&n,&m); if(n==0&&m==0)break;
	for(int i=0;i<n;i++){
		scanf("%d",&pi);
		p.push_back(pi);
	}
	p.push_back(0);

	for(int i=0;i<=n;i++){
		for(int j=0;j<=n;j++){
			x.push_back(p[i]+p[j]);
		}
	}

	sort(x.begin(),x.end());
	int ret=0;
	for(int i=0;i<n*n+2*n+1;i++){
		int t=upper_bound(x.begin(),x.end(),m-x[i])-x.begin();
		if(t>0)ret=max(ret,x[i]+x[t-1]);
	}

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