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 _, val := range sums {
			rem := M - val
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, val+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

C++: #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace::std;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define endl "\n"
typedef long long lint;
#define rep(i, n) for(lint i = 0; i < (lint)(n); i++)
#define repi(i,a,b) for(lint i=lint(a);i<lint(b);i++)
#define rrep(i, n) for(lint i = (lint)(n-1); i >-1; i--)
#define output(v) do{bool f=0;for(auto i:v){cout<<(f?" ":"")<<i;f=1;}cout<<"\n";}while(0)
#define all(x) (x).begin(),(x).end()
#define INF (1LL<<60)
#define MOD 1000000007
inline bool chmin(auto& s,const auto& t){bool res=s>t;s=min(s,t);return res;}
inline bool chmax(auto& s,const auto& t){bool res=s<t;s=max(s,t);return res;}
#define output(v) do{bool f=0;for(auto i:v){cout<<(f?" ":"")<<i;f=1;}cout<<"\n";}while(0)
#define output2(v) for(auto i:v)output(i);
vector<lint> dx={-1,1,0,0,1,1,-1,-1};
vector<lint> dy={0,0,-1,1,1,-1,1,-1};
int main(){
    while(1){
        lint n,m;
        cin>>n>>m;
        if(n==0&&m==0)break;
        vector<lint> a(n);
        rep(i,n)cin>>a[i];
        vector<lint> list(all(a));
        rep(i,n)rep(j,i+1){
            list.push_back(a[i]+a[j]);
        }
        list.push_back(0);
        sort(all(list));
        lint ans=0;
        for(lint val:list){
            auto itr=upper_bound(all(list),m-val);
            if(itr!=list.begin())chmax(ans,val+*prev(itr,1));
        }
        cout<<ans<<endl;
    }
}

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

long long kazu[1002001];//1001^2
long long n,m,Max = 0,data[1001];

int main(){
	while(std::cin>>n>>m, n * m){
		if(n==0&&m==0)break;

		memset(kazu, 0, sizeof(kazu));
		memset(data, 0, sizeof(data));

		for(int i=0;i<n;i++){
			std::cin >> data[i+1];
		}
		for(int c=0;c<n+1;c++){
			for(int d=0;d<n+1;d++){
				kazu[c*n+d]=data[c]+data[d];
			}
		}
		std::sort(kazu,kazu+(n+1)*(n+1));

		long long* ptr;
		for(int i = 0; i < (n + 1) * (n + 1);++i){
			if(kazu[i] < m){
				ptr = std::upper_bound(kazu, kazu + (n + 1) * (n + 1), m - kazu[i]) - 1;
				Max = std::max(Max, *ptr + kazu[i]);
			}
		}


		std::cout << Max << std::endl;
		Max = 0;
	return 0;
}

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

#define rep(i, n) for (int i=0; i<n; i++)
typedef long long int ll;

int N, M;
ll P[1111];
ll PP[11111111];
int p;

int main(int argc, char *argv[])
{
  while(cin >> N >> M, N){
    int NN = N * N;
    rep(i, N) cin >> P[i];
    rep(i, N) rep(j, N) PP[i + j * N] = P[i] + P[j];
    sort(P,  P  + N );
    sort(PP, PP + NN);
    ll res = 0;
    res = max(res,  P[upper_bound(P,  P  + N,  M) -  P - 1]);
    res = max(res, PP[upper_bound(PP, PP + NN, M) - PP - 1]);    
    rep(i, N) res = max(res, PP[upper_bound(PP, PP + NN, M -  P[i]) - PP - 1] +  P[i]);
    rep(i, NN) if((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0) res = max(res, PP[p] + PP[i]);
    rep(i, NN) if((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0) res = max(res, PP[p] + PP[i]);
    cout << res << endl;
  }
  
  
  return 0;
}

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

using namespace std;

int main()
{
	for(int n,m;scanf("%d%d",&n,&m),n;){
		int p[1001];	p[0]=0;
		for(int i=1;i<=n;i++)	scanf("%d",p+i);

		int nn=0;
		static int xp[1001*1001];
		for(int i=0;i<=n;i++)for(int j=i;j<=n;j++)	xp[nn++]=p[i]+p[j];
		sort(xp,xp+nn);

		int maxscore=0;
		for(int i=0;i<nn;i++){
			if(m-xp[i]>=0)
				maxscore=max(maxscore,xp[i]+upper_bound(xp,xp+nn,m-xp[i])[-1]);
		}
		printf("%d\n",maxscore);
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,m,a[1000];
int s[500500],stop;
int main(){
	while(true){
	cin>>n>>m;
	if(!n&&!m)break;
	stop=1,a[0]=0,s[0]=0;
	rep(i,n){
		cin>>a[i+1];
		rep(j,i+2)if(s[i+1]+s[j]<=m)s[stop]=a[i+1]+a[j],stop++;
	}
	sort(s,s+stop);
	int ans=0;
	rep(i,stop){
		int left=0,right=stop,mid,key=m-s[i];
		if(key>=0){
		while(left<right){
			mid=(left+right)/2;
			if(s[mid]<key)left=mid+1;
			else right=mid;
		}
		if(s[mid]>key)mid--;
		if(mid+1)ans=max(ans,s[mid]+s[i]);
		}
	}
	cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1002001];
int main(void){
	while(1){
		ll n,m,i,j,p[1001],hi,lo,mi,pp,x,mx;
		cin>>n>>m;
		if(n==0)break;
		p[0]=0;
		for(i=1;i<=n;i++) cin>>p[i];
		for(i=0;i<=n;i++) for(j=0;j<=n;j++) a[i*n+j]=p[i]+p[j];
		pp=pow(n+1,2);
		sort(a,a+(pp+2));
//		for(i=0;i<=pp+1;i++) cout<<a[i]<<endl;
		mx=0;
		for(i=0;i<=pp+1;i++){
			hi=pp+1; lo=0;
			while(hi>lo){
				mi=(hi+lo)/2;
				x=a[i]+a[mi];
				if(x>m) hi=mi-1;
				else if(x<m) lo=mi+1;
			}
			x=a[i]+a[lo];
			if(mx<x&&m>=x) mx=x;
		}
		cout<<mx<<endl;
	}
	return 0;
}

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

vector<long long int> p;
vector<long long int> p2;

int main(void){
	int a,b,n,m;
	while(1){
		long long int ans=0;
		scanf("%d%d",&n,&m);
		if(n==0 && m==0)break;
		p.clear();
		p2.clear();
		for(a=0;a<n;a++){
			int num;
			scanf("%d",&num);
			p.push_back(num);
		}
		sort(p.begin(),p.end());
		p2.push_back(0);
		for(a=0;a<n;a++){
			p2.push_back(p[a]);
			for(b=0;b<n;b++){
				p2.push_back(p[a]+p[b]);
			}
		}
		p2.push_back(0);
		sort(p2.begin(),p2.end());
		for(a=0;a<p2.size();a++){
			long long int res=m-p2[a];
			if(m-p2[a]<0)continue;
			long long int pp=*(lower_bound(p2.begin(),p2.end(),res)-1);
			if(ans<pp+p2[a])ans=pp+p2[a];
			//cout << pp+p2[a] << endl;
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int n,m;
vector<int> p;
vector<Int> two;

int main() {
	while(cin>>n>>m,n){
		p.clear();
		two.clear();
		int i;
		for(i=0;i<n;i++){
			p.push_back(in());
		}
		int j;
		for(i=0;i<n;i++){
			for(j=0;j<n;j++){
				two.push_back(p[i]+p[j]);
			}
			two.push_back(p[i]);
		}
		two.push_back(0);
		sort(two.begin(),two.end());
		Int res=0;
		for(i=0,j=two.size()-1;i<two.size()&&j>=0;){
			if(two[i]+two[j]<=m){
				chmax(res,two[i]+two[j]);
				i++;
			}else{
				j--;
			}
		}
		cout<<res<<endl;
	}
	return 0;
}