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+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>

#define MAX_N 1000

using namespace std;

typedef long int int64;

int64 N,M,P[MAX_N];
vector<int64> T,S;

int main(void)
{
	while(true)
	{
		T.clear();
		S.clear();
		for(int i = 0;i < MAX_N;i++)
			P[i] = 0;
		cin >> N >> M;
		if(N == 0 && M == 0)
			exit(0);
		for(int i = 0;i < N;i++)
			cin >> P[i];
		T.push_back(0);
		for(int i = 0;i < N;i++)
		{
			for(int ii = 0;ii < N;ii++)
				T.push_back(P[i] + P[ii]);
		}
		sort(T.begin(),T.end());
		int64 prev = -1;
		for(int i = 0;i < N*N;i++)
		{
			if(prev != T[i])
			{
				S.push_back(T[i]);
				prev = T[i];
			}
		}
		sort(S.begin(),S.end());
		int64 maxscore = 0;
		int64 sc = 0;
		for(auto itr = S.begin();itr < S.end();itr++)
		{
			sc = *(upper_bound(S.begin(),S.end()-1,M-(*itr))-1) + *itr;
			maxscore = max(maxscore,sc);
		}
		cout << maxscore << endl;
	}
}

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<map<ll,bool>> dp(5);

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<map<ll,bool>>(5);
    dp[0][0] = true;
    rep(i,4){
      for(auto &&e : dp[i]){
        if(e.se){
          dp[i+1][e.fi] = true;
          rep(k,n){
            if(e.fi+s[k] <= m)
              dp[i+1][e.fi+s[k]] = true;
          }
        }
      }
    }
    ll ans = -1;
      for(auto rit = dp[4].rbegin(); rit != dp[4].rend(); ++rit){
        if(rit->se){
          ans = max(ans,rit->fi);
          break;
        }
      }
    cout << ans << endl;
  }
  return 0;
}

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

int main(){
  int i,j,k,l,n,m,a[1001],s;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    a[0] = 0;
    for(i=1;i<=n;i++) cin >> a[i];
    sort(a,a+n+1);

    s = 0;
    for(i=0;i<=n;i++){
      if(a[i] > m) break;
      for(j=0;j<=n;j++){
	if(a[i] + a[j] > m) break; 
	for(k=0;k<=n;k++){
	  if(a[i] + a[j] + a[k] > m) break;
	  for(l=0;l<=n;l++){
	    if(a[i] + a[j] + a[k] + a[l] > m) break;
	    s = max(s,a[i] + a[j] + a[k] + a[l]);
	  }
	}
      }
    }
    cout << s << 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<ll>> dp(5,vector<ll>(1e10*2,0));
vary(ll,s,1001,0);
ll dfs(ll cnt, ll sum){
  if(dp[cnt][sum] != inf || cnt == 4) return sum;
  dp[cnt][sum] = 1;
  ll res = sum;
  rep(i,n){
    if(sum + s[i] <= m){
      res = max(res,dfs(cnt+1,sum + s[i]));
    }
  }
  return res;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(cin >> n >> m && n && m){
    rep(i,n){
      cin >> s[i];
    }
    sort(s.begin(),s.begin()+n);
    rep(i,5){
      fill(all(dp[i]),inf);
    }
    cout << dfs(0,0) << endl;
  }
  return 0;
}