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++: #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;
}

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

int main() {
  while(true) {
    int N, M; cin >> N >> M;
    if(N == 0 && M == 0) break;
    vector<int> A(N);
    for(int i = 0; i < N; i++) {
      cin >> A[i];
    }

    A.push_back(0);
    vector<int> B(N*N);
    for(int a : A) {
      for(int b : A) {
        B.push_back(a+b);
      }
    }
    sort(B.begin(), B.end());
    B.erase(unique(B.begin(), B.end()), B.end());

    int ans = 0;
    for(int i = 0; i < (int)B.size(); i++) {
      if(B[i] > M) continue;
      if(B[i] == M) { ans = M; continue; }
      ans = max(ans, B[i] + *(lower_bound(B.begin(), B.end(), M - B[i]) - 1));
    }
    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>

using namespace std;

int n,m;
int p[1001];

int solve(){
  int pp[(n+1)*(n+1)];
  
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      pp[n*i+j] = p[i] + p[j];
      // cout<<"pp:"<<pp[n*i+j]<<endl;
    }
  }
  /*
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      cout<<"ij:"<<n*i+j<<"\t"<<pp[i*n+j]<<endl;
    }
  }
  */
  int ans = 0;
  
  sort(pp,pp+n*n+n+1);
  /*
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      cout<<"ij:"<<n*i+j<<"\t"<<pp[i*n+j]<<endl;
    }
  }
  */
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      int* it = upper_bound(pp,pp+n*n+n+1,m-p[i]-p[j]);
      --it;
      // cout<<"it"<<*it<<endl;
      // cout<<"ij:"<<p[i]<<"\t"<<p[j]<<endl;
      // cout<<"ans:"<<p[i]+p[j]+(*it)<<endl;
      if(p[i] + p[j] > m)continue;
      ans = max(ans,p[i]+p[j]+(*it));
    }
  }
  return ans;
}

int main(){
  while(cin>>n>>m){
    if(n==0&&m==0)break;
    p[0] = 0;
    for(int i = 1;i<=n;i++){
      cin>>p[i];
    }
    
    cout<<solve()<<endl;
  }
  return 0;
}