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 <cstring>
using namespace std;

int n, m, point1[1001], sum, ans;
vector<int> point2;

int solve(int start, int end, int border){
    int mid;
    while(1){
        if((end + start) % 2){
            mid = (end + start) / 2 + 1;
        } else{
            mid = (end + start) / 2;
        }

        if(point2[mid] > border){
            end = mid;
        } else{
            start = mid;
        }

        if(end - start == 1){
            if(point2[end] < border){
                return point2[end];
            } else{
                return point2[start];
            }
        }
    }
}

int main(){
    while(cin >> n >> m && n && m){
        for(int i = 0; i < n; i++){
            cin >> point1[i];
        }
        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < n + 1; j++){
                point2.push_back(point1[i] + point1[j]);
            }
        }

        sort(point1, point1 + n + 1);
        sort(point2.begin(), point2.end());

        for(int i = 0; i < n + 1; i++){
            if(point2[i] > m){
                continue;
            }
            ans = max(ans, point2[i] + solve(0, point2.size() - 1, m - point2[i]));
        }

        cout << ans << endl;

        memset(point1, 0, sizeof(point1));
        point2.clear();
        ans = 0;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
  
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
  
typedef long long ll;
typedef pair<int, int> ii;
 
#define DEBUG false
 
int main(){
 
    ll n, m;
    while(cin >> n >> m){
        if(n == 0 && m == 0) break;
        vector<ll> p(n+1);
        rep(i, n) cin >> p[i];
         
        rep(i, n){
            REP(j, i, n){
                p.push_back(p[i] + p[j]);
            }
        }
         
        SORT(p);
         
        ll ans = 0;
 
        rep(i, p.size()){
            auto it2 = upper_bound(p.begin(), p.end(), m - p[i]);
            if(it2 == p.begin()) continue;
            it2--;
            if(p[i] > *it2) break;
            ans = max(ans, p[i] + *it2);
        }
         
        cout << ans << endl;
         
    }
     
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int s[501501],a[1001],n,m,t;
int BNS(int key){
	int ret=0,ran=t/2,nse=0;
	while(true){
		nse+=ran;
		if(s[nse]<=key)ret=max(ret,nse);
		else nse-=ran;
		ran/=2;
		if(!ran)break;
	}
	return ret;
}
int main(){
	while(true){
	cin>>n>>m;
	if(!n&&!m)break;
	a[0]=0,t=1,s[0]=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		for(int j=0;j<=i;j++)if(a[i]+a[j]<=m)s[t]=a[i]+a[j],t++;
	}
	sort(s,s+t);
	int ans=0;
	for(int i=0;i<t;i++)ans=max(ans,s[i]+s[BNS(m-s[i])]);
	cout<<ans<<endl;
	}
	return 0;
}

C++: #ifdef LOCAL
    #define _GLIBCXX_DEBUG
    #define __clock__
#else
    #pragma GCC optimize("Ofast")
#endif
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;
using PII = pair<ll, ll>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define p_yes() p("YES")
#define p_no() p("NO")
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())
#define MP make_pair

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N, M;
    while(true){
      cin>>N>>M;
      if(N==0) return 0;

      VI A(N);
      rep(i, N) cin>>A[i];
      
      A.push_back(0);
      N++;

      VI V;
      rep(i, N){
        rep(j, N){
          ll c_d = A[i]+A[j];
          V.push_back(c_d);
        }
      }
      SORT(V);

      ll ma = 0;
      rep(i, N){
        rep(j, N){
          ll a_b = A[i]+A[j];
          if(a_b<=M){
            ll rest = M-a_b;
            if(rest==0){
              chmax(ma, M);
              continue;
            }
            auto it = lower_bound(ALL(V), rest);
            if(it==V.end()) continue; // NOT FOUND
            if(*it==rest){
              // just value
              chmax(ma, M);
            }else{
              // over
              it--;
              ll sum = a_b + *it;
              chmax(ma, sum);
            }
          }
        }
      }
      p(ma);
    }
    
    return 0;
}