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 <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(m);
			}
		}
		sort(data.begin(), data.end());
		vector<long long int> data2;
		for (int i = 0; i < data.size(); i++) {
			for (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 (int i = 0; i < data2.size(); i++) {
			for (int j = i; j < data2.size(); j++) {
				if (data2[i]+data2[j] <= m) {
					data.push_back(data2[i]+data2[j]);
				} else {
					break;
				}
			}
		}
		data2.clear();
		sort(data.begin(), data.end());
		vector<long long int> data2;
		for (int i = 0; i < data.size(); i++) {
			for (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());
		long long int sz = data2.size();
		cout << data2[sz-1] << endl;
	}
	return 0;
}

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

using namespace std;

vector<int> mato;
vector<int> a;

int main(){
	int n,m;
	int self;
	while(scanf("%d %d",&n,&m) && n && m){
		mato.clear();
		a.clear();
		
		for(int i=0;i<n;i++){
			scanf("%d",&self);
			mato.push_back(self);
		}
		mato.push_back(0);
		mato.erase(unique(mato.begin(),mato.end()),mato.end());
		int s = mato.size();
		for(int i=0;i<s;i++){
			for(int j=0;j<s;j++){
				a.push_back(mato[i] + mato[j]);
			}
		}
		a.erase(unique(a.begin(),a.end()),a.end());
		int max = 0;
		s = a.size();
		for(int i=0;i<s;i++){
			for(int j=0;j<s;j++){
				if(a[i] + a[j] < m && a[i] + a[j] > max){
					max = a[i] + a[j];
				}
			}
		}
		printf("%d\n",max);
	}
}

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

vector<long long> v,v2;

bool isOK(int index, int key){
  /*indexが条件を満たすかどうかを判定(今回はkeyよりおおきいか)*/
  if(v2[index]<=key) return true;
  else return false;
}

int binary_search(int key){
  int ng = (int)v2.size();
  int ok = -1;
  //okとngの初期値によって最大値だけでなく最小値も求められる.
  //ng=-1の時はkey以上の数値の最小値

  while(abs(ok - ng) > 1){
    int mid = (ok + ng) / 2;

    if(isOK(mid,key)) ok = mid;
    else ng = mid;
  }

  return ok;
}


int main(){
  long long n,m;
  while(cin >> n >> m){
    if(n==0)break;
    v.resize(n+1);
    v[0]=0;
    long long i,j;
    for (i = 1; i < n+1; i++) {
      cin >> v[i];
    }
    v2.resize((n+1)*(n+2)/2);
    for(i=0;i<n+1;i++){
      for(j=i;j<n+1;j++){
        v2.emplace_back(v[i]+v[j]);
      }
    }
    sort(v2.begin(),v2.end());
    long long ret=0;
    for(i=0;i<v2.size();i++){
      if(v2[i]>m)break;
      int key=binary_search(m-v2[i]);
      if(key!=-1)ret=max(ret,v2[i]+v2[key]);
    }
    cout << ret << endl;
  }
  return 0;
}

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; });
      cmax(ans, v[i] + v[j]);
    }
    cout << ans << endl;
  }
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define vi vector<int>
#define INF 999999999


int main(){
    
    int n,m;
    while(cin>>n>>m&&n&&m){
        vi p(n);
        rep(i,n)cin>>p[i];
        sort(all(p));
        
        set<ll> val;
        val.insert(0);
        rep(i,n){
            if(p[i]>m)break;
            val.insert(p[i]);
        }
        rep(i,n)for(ll j=i;j<n;j++){
            if(p[i]+p[j]>m)break;
            val.insert(p[i]+p[j]);
        }
        
        vector<ll> left,right;
        
        for(auto &v : val) left.pb(v);
        
        
        sort(all(left));
        right = left;
        
        ll maxi=0;
        rep(i,left.size()){
            ll num = left[i];
            if(num>m)break;
            
            ll l=0, r=right.size()+1, mid=(l+r)/2LL;
            while(l+1<r){
                if(right[mid]<=m-num) l = mid;
                else    r= mid;
                mid = (l+r)/2LL;
            }
            
            maxi = max<ll>(maxi,num+right[mid]);
        }
        cout<<maxi<<endl;
    }
}