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++: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
#include <cassert>
using namespace std;

//common
#define BR "\n"

#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))

typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;

//config
#define MODE_DEBUG
//#define INF 1<<30
//#define EPS 1e-8
//const ll MOD =100000007;

//debug
#ifdef MODE_DEBUG
#define  DUMP(x)  cerr << #x << " = " << (x) <<endl
#define DEBUG(x) DUMP(x) << " (L" << __LINE__ << ")" << " " << __FILE__<<endl
#define CHECK(exp,act)  if(exp!=act){DUMP(exp);DEBUG(act);}
#define STOP(e)  CHECK(e,true);if(!(e)) exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp,act)
#define STOP(e)
#endif

template<class T> inline string toString(const vector<T>& x) {
	stringstream ss;
	REP(i,x.size()){
		if(i!=0)ss<<" ";
		ss<< x[i];
	}
	return ss.str();
}
template<class T> inline string toString(const vector<vector<T>>& map) {
	stringstream ss;
	REP(i,map.size()){
		if(i!=0)ss<<BR;
		ss<< toString(map[i]);
	}
	return ss.str();
}
template<class K,class V>  string toString(map<K,V>& x) {
	string res;stringstream ss;
	for(auto& p:x)ss<< p.first<<":" << p.second<<" ";
	return ss.str();
}

template<typename T,typename V> inline T mod(T v,V MOD){
	return (v%MOD+MOD)%MOD;
}


int main(){
	while(true){
		int N;ll M;cin >> N >> M;
		if(N==0 && M ==0)break;
		vector<ll> ps(N+1);//0 
		REP(i,N)cin >>ps[i];
		sort(ALL(ps));
		vector<ll> ps2((N+1)*(N+1));
		REP(i,(N+1))REP(j,(N+1))ps2[i*(N+1)+j]=ps[i]+ps[j];
			
		ll Mv=0;
		for(int i=0;i<(N+1)*(N+1);i++){
			int v1=ps2[i];

			int l=0,r=(N+1)*(N+1);
			while(r-l>1){
				int m=(l+r)/2;
				if(v1+ps2[m]<=M){
					Mv=max(Mv,v1+ps2[m]);
					l=m;
				}else{
					r=m;
				}
			}
		}
		cout << Mv<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 1000000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

ll n,m;
vector<vector<bool>> dp(5,vector<bool>(2 * 1e8,0));

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(cin >> n >> m && n && m){
    vary(ll,s,1001,0);
    rep(i,n){
      cin >> s[i];
    }
    sort(s.begin(),s.begin()+n);
    dp = vector<vector<bool>>(5,vector<bool>(2 * 1e8,0));
    dp[0][0] = true;
    rep(i,4){
      rep(j,2 * 1e8+1){
        if(j > m) break;
        if(dp[i][j]){
          dp[i+1][j] = true;
          rep(k,4){
            if(j+s[k] <= m)
              dp[i+1][j+s[k]] = true;
          }
        }
      }
    }
    for(int i = m; i >= 0; --i){
      if(dp[4][i]){
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <map>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long lld;
typedef pair<lld, int> Pair;

int main( void )
{
	int n, m, input[4001];
	map<lld, int> field;
	while(cin >> n >> m, n | m) {
		field.clear();
		int tMax = 0;
		memset(input, 0, sizeof(input));
		for(int i=0; i < n; ++i) {
			cin >> input[i];
			for(int j=1; j <= 4; ++j) {
				lld tmp = input[i] * j;
				if(tmp > m || m < 0) {
					input[(j-1)*n + i] = 0;
					continue;
				}
				else
					input[(j-1)*n + i] = tmp;
				if(!field.count(tmp))
					field.insert(Pair(tmp, j));
				else
					field[tmp] = min(field[tmp], j);
				//cout << tmp << ' ';
			}
			//cout << endl;
			tMax = max(tMax, input[i]);
		}
		int sub = m;
		for(int i=1; i <= 4; ++i) {
			int nn = m - tMax * i;
			if(nn >= 0) sub = min(sub, nn);
		}
		n *= 4;
		input[n++] = 0;
		for(int i=n; i > 0; --i) {
			if(!input[i-1]) n = i+1;
		}
		//cout << "sub = " << sub << endl;
		field.insert(Pair(0, 0));
		sort(input, input+n, greater<int>());
		int Max = 0;
		for(int i=0; i < n; ++i) {
			for(int j=i; j < n; ++j) {
				int ii = input[i], jj = input[j];
				int count = field[ii] + field[jj];
				if(count <= 4) {
					int nn = m - ii + jj;
					int start = nn - sub, end = nn + sub + 1;
					for(int k=start; k < end; ++k) {
						if(k >= 0) {
							if(field.count(k)) {
								if(count + field[k] <= 4) {
									int tt = ii + jj + k;
									if(tt <= m) {
										Max = max(Max, tt);
										sub = m - Max;
									}
									//cout << ii << ' ' << jj << ' ' << k << ' ' << count << endl;
								}
							}
						}
					}
				}
			}
		}
		cout << Max << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int, bool> PIB;
typedef vector<int> VI;
typedef VI::iterator VII;

int n, m, ans = 0;
VI target, target2;

int main()
{
  while(true){
    scanf("%d%d", &n, &m);
    if(!n && !m)
      break;
    target.clear();
    target2.clear();
    ans = 0;

    target.push_back(0);
    for(int i = 0; i < n; i++){
      int p;
      scanf("%d", &p);
      target.push_back(p);
    }
    
    for(int i = 0; i < target.size(); i++)
      for(int j = 0; j < target.size(); j++)
	target2.push_back(target[i] + target[j]);
    
    sort(target2.begin(), target2.end());

    for(int i = 0; i < target2.size(); i++){
      VII p = upper_bound(target2.begin(), target2.end(), m - target2[i]);
      if(p != target2.begin())
	ans = max(ans, target2[i] + *(p - 1));
    }
    
    printf("%d\n", ans);
  }
  return 0;
}