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<bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

typedef std::pair<int,int> PII;


int N, M;
VI P(1000);
VI sum(1000005);

int main() {
    while(std::cin >> N >> M) {
        if(N == 0 && M == 0) break;
        REP(i, N) {
            std::cin >> P[i];
        }

        int cnt = 0;
        for(int i=0; i<=N; ++i) {
            for(int j=0; j<=N; ++j) {
                int s= 0;
                s += (i < N ? P[i] : 0);
                s += (j < N ? P[j] : 0);
                if(s > M) continue;
                sum[cnt++] = s;
            }
        }
        std::sort(sum.begin(), sum.begin() + cnt);



        int ans = 0;
        for(int i=0; i<cnt; ++i) {
            int v2 = *(std::upper_bound(sum.begin(), sum.begin()+cnt, M - sum[i]) - 1);
            ans = std::max(ans, sum[i] + v2);
        }
        std::cout << ans << std::endl;
    }
}

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

int max(int x, int y);
int add(int x, int y, int m);

int main(){
  int n, m;
  vector<int> ans;
  while(cin >> n >> m, n&&m){
    int dp[4][n];
    for(int i = 0; i < 4; i++){
      for(int j = 0; j < n; j++){
	dp[i][j]  = 0;
      }
    }
    for(int i = 0; i < n; i++){
      cin >> dp[0][i];
    }

    for(int i = 1; i < 4; i++){
      for(int j = 0; j < n; j++){
	dp[i][j] = dp[i-1][j];
	for(int k = 0; k < n; k++){
	  dp[i][j] = max(add(dp[i-1][j], dp[0][k], m), dp[i][j]);
	}
      }
    }
    int max = 0;
    for(int i = 0; i < n; i++){
      if(dp[3][i] > max) max = dp[3][i];
    }
    ans.push_back(max);
  }
  vector<int>::iterator it;
  for(it = ans.begin(); it != ans.end(); it++){
    cout << *it;
    if(it != --ans.end()) cout << " ";
  }
  cout << endl;
}


int max(int x, int y){
  return x < y ? y : x;
}

int add(int x, int y, int m){
  return x+y < m ? x+y : 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
while(1){
int i,j,n,m,o=1,s=0;
static int p[1001]={0},up[1001*1001]={0};
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++){
scanf("%d",&p[i]);
for(j=i;j>=0;j--){
up[o]=p[i]+p[j];
o++;
}
}
sort(up,up+o);
for(i=0;i<o;i++){
if(up[i]+*(upper_bound(up,up+o,m-up[i])-1)<=m)s=max(s,up[i]+*(upper_bound(up,up+o,m-up[i])-1));
}
printf("%d\n",s);
}
return 0;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

typedef vector<int> vi;

int main() {
	while (true) {
		int n, m, score[1001];
		vi v;
		cin >> n >> m;
		if (n == 0 && m == 0) break;
		REP(i, n) scanf("%d", &score[i]);
		score[n++] = 0;

		REP(i, n) FOR(j, i, n) if (score[i] + score[j] <= m)
			v.push_back(score[i] + score[j]);

		sort(v.begin(), v.end());
		unique(v.begin(), v.end());

		int ans = 0;
		REP(i, v.size()) {
			if (m - v[i] < 0) continue;
			vi::iterator it = upper_bound(v.begin(), v.end(), m - v[i]);
			if (it != v.begin())
				ans = max(ans, *(it - 1) + v[i]);
		}

		cout << ans << endl;
	}
	return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
//http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
#include<cstring>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define P pair<int,int>
#define ll long long 

ll n,m;
ll t[10000];
const int MA=1000000;
ll s1[MA];
ll s2[MA];


int main(){

while(cin>>n>>m,n||m){
rep(i,MA)s1[i]=s2[i]=0;
rep(i,10000)t[i]=0;

for(int i=1;i<=n;i++)cin>>t[i];
int g=0;

for(int i=0;i<=n;i++){
	for(int j=0;j<=n;j++){
		s1[i*(n+1)+j]=t[i]+t[j];
		g++;
	}
}


sort(s1,s1+g);
rep(i,g)s2[i]=s1[i];

int ans=0;

for(int i=0;i<g;i++){
	int now=s1[i];

	int l=0;
	int r=g-1;
	int mid=(l+r)/2;
	int x=s2[mid];

	for(int i=0;i<70;i++){
		if(x+now==m)break;
		if(l>r)break;
		if(x+now>m){
			r=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
		else{
			l=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
	}

	//cout<<" "<<x<<" "<<now<<" "<<x+now<<endl;
	if(x+now<=m)
	ans=max(ans,x+now);
}
cout<<ans<<endl;
}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T>
int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

template <class T> void max_swap(T& a, const T b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;


int main()
{
	int n;
	ll m;
	while (scanf("%d%lld", &n, &m), n | m)
	{
		ll p[1024];
		for (int i = 0; i < n; ++i)
			scanf("%lld", p + i);
		p[n] = 0;
		vector<ll> two;
		for (int i = 0; i <= n; ++i)
			for (int j = i; j <= n; ++j)
				two.push_back(p[i] + p[j]);
		sort(all(two));

		ll res = 0;
		foreach (it, two)
		{
			int k = lower_bound(all(two), m - *it) - two.begin();
			--k;
			if (k >= 0)
				max_swap(res, *it + two[k]);
		}
		printf("%lld\n", res);
	}
}