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 <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);
}
}

C++: #include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define Rep(i, n) for (long long i = 0; i< (long long)(n); i++)
#define RRep(i,n) for (long long i = ((long long)(n)-1); i>=0; i--)

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;

template <class T>
inline bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

ll INF = 1e9+7;

vector<int> dx = {-1,0,1,0};
vector<int> dy = {0,-1,0,1};

template<class Abel> struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<Abel> diff_weight;

    UnionFind(int n = 1, Abel SUM_UNITY = 0) {
        init(n, SUM_UNITY);
    }

    void init(int n = 1, Abel SUM_UNITY = 0) {
        par.resize(n); rank.resize(n); diff_weight.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            diff_weight[x] += diff_weight[par[x]];
            return par[x] = r;
        }
    }

    Abel weight(int x) {
        root(x);
        return diff_weight[x];
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y, Abel w) {
        w += weight(x); w -= weight(y);
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y), w = -w;
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        diff_weight[y] = w;
        return true;
    }

    Abel diff(int x, int y) {
        return weight(y) - weight(x);
    }
};

int solve(){
    ll n,m;
    cin >> n >> m;
    if(n==0 && m == 0) return 1;
    vector<ll> p(n);
    Rep(i,n) cin >> p[i];
    Rep(i,n) Rep(j,n){
        p.emplace_back(p[i]+p[j]);
    }
    sort(p.begin(),p.end());
    ll ans = 0;
    Rep(i,p.size()){
        ll x = m-p[i];
        if(x<0) break;
        auto y = lower_bound(p.begin(),p.end(),x);
        if(y-p.begin()==0) chmax(ans,p[i]);
        else{
            chmax(ans,p[i]+p[y-p.begin()-1]);
        }
    }
    std::cout << ans << endl;
    return 0;
}

int main(){
    int a = 0;
    while(a==0){
        a = solve();
    }
}