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++: #define	_USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <set>
#include <complex>
using namespace std;
int main(){
	
	int n;
	long long int m;
	while(cin>>n>>m,n!=0&&m!=0){
	
	vector<long long> v(n+1,0);
	vector<long long> s;

	
	for(int i=1;i<=n;i++)
		cin>>v[i];
		
		for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++){
				s.push_back(v[i]+v[j]);
			}
		}
	
	
	
	sort(s.begin(),s.end());
	long long ans=0;
		for(int i=0;i<s.size();i++){
	vector<long long>::iterator it=upper_bound(s.begin(),s.end(),m-s[i]);
			if(it!=s.begin()){
				it--;
			ans=max(ans,s[i]+s[it-s.begin()]);
			}
		}
	cout<<ans<<endl;
	}

	
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(true){
		cin>>n>>m;
		if(n==0)	return 0;
		vector<int> p(n);
		for(int i=0;i<n;i++)	cin>>p[i];
		p.push_back(0);	n++;
		set<int> s;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++)	if(p[i]+p[j]<m)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(auto it=s.begin();it!=s.end();it++){
			auto it2=s.lower_bound(*it);
			if(it2!=s.begin())	it2--;
			if(*it+*it2<=m)	ans=max(ans,*it+*it2);
			else 	break;
		}
		cout<<ans<<endl;
	}
}

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

int main(){
	int n, m;
	while(scanf("%d%d", &n, &m), n){
		vector<int> p(n + 1), v;
		v.reserve(1000000);
		for(int i = 1; i <= n; ++i){
			scanf("%d", &p[i]);
			for(int j = 0; j <= i; ++j){
				int s = p[i] + p[j];
				if(s <= m){	v.push_back(s); }
			}
		}
		sort(v.begin(), v.end());

		int ans = 0;
		for(int i = 0; i < v.size(); ++i){
			ans = max(ans, v[i] + upper_bound(v.begin(), v.end(), m - v[i])[-1]);
		}
		printf("%d\n", ans);
	}
}

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

int main(int argc, char *argv[]){
  int n, m;
  while(cin >> n >> m, n){
    int point[n + 1];
    point[0] = 0;
    set<int> a;
    for (int i = 1; i <= n; i++) {
      cin >> point[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a.insert(- point[i] - point[j]);
      }
    }
    int ans = 0;
    for (int i: a){
      ans = max(ans, - i - *(a.upper_bound(- m - i)));
    }
    std::cout << ans << std::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[1000],ans;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    for(i=0;i<n;i++) cin >> a[i];

    sort(a,a+n);
    ans = 0;

    for(i=n-1;i>=0;i--){
      if(a[i] <= m) ans = max(ans,a[i]);

      if(a[i] < m){
	for(j=n-1;j>=0;j--){
	  if(a[i] + a[j] <= m) ans = max(ans,a[i] + a[j]);
	  
	  if(a[i] + a[j] < m){
	    for(k=n-1;k>=0;k--){
	      if(a[i] + a[j] + a[k] <= m) ans = max(ans,a[i] + a[j] + a[k]);
	      
	      if(a[i] + a[j] + a[k] < m){
		for(l=n-1;l>=0;l--){
		  if(a[i] + a[j] + a[k] + a[l] <= m){
		    ans = max(ans,a[i] + a[j] + a[k] + a[l]);
		    break;
		  }
		}
	      }
	    }
	  }
	}
      }
    }

    cout << ans << endl;

  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long int n,m;
	while(cin>>n>>m,n!=0||m!=0){
	long long int s=0;
		vector<long long int> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
			}
			else
				n--;
		}
		cout<<s<<endl;
	}
	
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
    int n;
    ll m;
    while(cin>>n>>m,n){
        vll v(n);
        rep(i,n){
            cin>>v[i];
        }
        sort(ALL(v));
        vll x;
        x.PB(0LL);
        rep(i,n){
            // x.insert(v[i]);
            x.PB(v[i]);
        }
        rep(i,n){
            rep(j,n){
                // x.insert(v[i]+v[j]);
                x.PB(v[i]+v[j]);
            }
        }
        sort(ALL(x));
        x.erase(unique(ALL(x)),x.end());
        // sort(ALL(x));
        ll ans=0;
        rep(i,x.size()){
            ll t=m-x[i];
            if(t<0) continue;
            auto it=upper_bound(ALL(x),t);
            if(it==x.begin()) continue;
            it--;
            maxs(ans,*it + x[i]);
        }
        cout<<ans<<endl;
    }
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
      cout<<fixed<<setprecision(20);
    mainmain();
}