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++: // clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

/* Find the maximum x∈[low, up) such that f(x) is true
    ---*     (true)
        ---- (false)
*/
template<typename F> int binarySearchL(int low, int up, const F &f) {
  while (up - low > 1) {
    int mid = low + (up - low) / 2;
    (f(mid) ? low : up) = mid;
  }
  return low;
}

main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> p(n);
    cin >> p;
    p.emplace_back(0), ++n;
    vector<int> v;
    v.reserve(n * n);
    rep(i, n) rep(j, n) v.emplace_back(p[i] + p[j]);
    sort(all(v));
    int ans = 0;
    rep(i, v.size()) {
      int j = binarySearchL(0, v.size(), [&](int k) { return v[i] + v[k] <= m; });
      if (v[i] + v[j] <= m) cmax(ans, v[i] + v[j]);
    }
    cout << ans << endl;
  }
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int a,s,d[1000],f[210000000];
int main(void)
{
	int high,low,ba,naka,nokori,kore,g,h,i,j,z,x,c,v;
	while(1){
		kore=0;
		scanf("%d %d",&a,&s);
		if(a==0&&s==0)
			break;
		d[0]=0;
		for(i=1;i<=a;i++){
			scanf("%d",&d[i]);
		}
		g=0;
		for(i=0;i<=a;i++){
			for(j=i;j<=a;j++){
				f[g]=d[i]+d[j];
				g++;
			}
		}
		sort(f,f+g);
		/*for(i=0;i<g;i++)
			printf("%d\n",f[i]);*/
		for(i=0;i<g;i++){
			v=0;
			low=0;
			high=g-1;
			nokori=s-f[i];
		//	printf("%d %d\n",i,nokori);
			if(nokori>=0){
				while(1){
					if(low>high)
						break;
					naka=(low+high)/2;
				//	printf("%d = %d %d %d %d %d\n",i,f[high],f[naka],f[low],f[i],nokori);
					if(nokori>f[naka]){
						low=naka+1;
					//	printf("%d %d %d low\n",i,nokori,f[naka]);
					}
					if(nokori<f[naka]){
						high=naka-1;
					//	printf("%d %d %d high\n",i,nokori,f[naka]);
					}
					if(nokori==f[naka]){
						ba=f[naka];
						v=1;
						break;
					}
				}
				if(v==0)
					ba=f[low-1];
				//printf("%d\n",f[i]+ba);
				if(kore<f[i]+ba){
					kore=f[i]+ba;
				//	printf("   %d\n",kore);
				}
			}
		}
		printf("%d\n",kore);
	}
	return 0;
}

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;
	cin >> 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<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 999999
#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<k;i++)
typedef long long ll;
typedef unsigned long long ull;

int main(){
    int n,m;
    vector<int> vi;
    while(scanf("%d%d",&n,&m),n,m){
        ll num[1010]={0};
        vi.clear();
        rep(i,n)scanf("%lld",&num[i]);
        rep(i,n+1)rep(j,n+1)if(num[i] + num[j] <= m)vi.push_back(num[i] + num[j]);
        sort(vi.begin(),vi.end());
        int len = vi.size();
        ll ans = -99999;
        rep(i , len){
            ll res = m-vi[i];
            if(m - vi[i] < 0)continue;
            ll  p = *(lower_bound(vi.begin(),vi.end(),res)-1);
            ans = max(ans,p+vi[i]);
        }
        printf("%d\n",ans);
    }
	return 0;
}