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<algorithm>
#include<vector>
using namespace std;
 
int n, m;
vector<int> p1, p2;
int main()
{
  while(cin >> n >> m && n && m){
    p1.clear();
    p2.clear();
     
    //chap1
    p1.push_back(0);
    for(int i = 0; i < n; i++){
      int p;
      cin >> p;
      p1.push_back(p);
    }
 
    //chap2
    for(int i = 0; i <= n; i++)
      for(int j = i; j <= n; j++)
    p2.push_back(p1[i] + p1[j]);
 
    sort(p2.begin(), p2.begin() + p2.size());
 
    //chap3
    int ans = 0;
    for(int i = 0; i < p2.size(); i++){
      vector<int>::iterator count2 = upper_bound(p2.begin(), p2.end(), m - p2[i]);
      if(count2 != p2.begin())
    ans = max(ans, *(count2 - 1) + p2[i]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

int main(void){
	while(1){
		int n, m; cin >> n >> m;
		if(n == 0 && m == 0) return 0;
		vector<int> p(n);
		rep(i, n) cin >> p[i];
		p.push_back(0);//??¢????????????????????\??????
		vector<int> pp((n + 1) * (n + 1));
		int cnt = 0;
		rep(i, n + 1)rep(j, n + 1){
			pp[cnt] = p[i] + p[j];
			cnt++;
		}
	
		sort(pp.begin(), pp.end());
		int ans = 0;
		//?¨???????(n^2*log(n*n))
		rep(i, (n + 1) * (n + 1)){
			int tmp = pp[i];
			int aim = m - tmp;
			if(aim < 0) continue;
			tmp += *(upper_bound(pp.begin(), pp.end(), aim) - 1);
			if(tmp <= m) ans = max(ans, tmp);
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
	vector<int> sum;
	int n,m;
	int a[1000];
	while(cin>>n>>m){
		int ans=0;
		a[0]=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a,a+n+1);
		for(int i=0;i<=n;i++){
			for(int j=i;j<=n;j++){
				if(m>=a[i]+a[j])sum.push_back(a[i]+a[j]);
			}
		}
		sort(sum.begin(),sum.end());
		for(int i=0;i<sum.size();i++){
			for(int j=i;j<sum.size();j++){
				if(sum[i]+sum[j]<=m){
					ans=max(ans,sum[i]+sum[j]);
				}
			}
		}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define min_3(a,b,c) min(a,min(b,c))
#define max_3(a,b,c) max(a,max(b,c))
#define mp1(a,b,c) P1(a,P(b,c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a,vector<a>,greater<a>>

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
const int kaijou[10]={1,1,2,6,24,120,720,5040,40320,362880};

int main(){
	while(1){
		int n,m;
		int p[1002];
		
		scanf("%d%d",&n,&m); if(n == 0 && m == 0)break;
		rep(i,n){
			scanf("%d",&p[i]);
		}
		
		vector<int> p1;
		rep(i,n){
			p1.pb(p[i]);
		}
		p1.pb(0);
		
		vector<int> p2;
		rep(i,n+1){
			rep(j,n+1){
				p2.pb(p1[i]+p1[j]);
			}
		}
		sor(p2);
		uniq(p2);
		
		int ret = 0;
		rep(i,p2.size()){
			if(p2[i] <= m){
				ret = max ( ret , p2[i] + *--ub(p2,m-p2[i]) );
			}
		}
		
		printf("%d\n",ret);
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
	int n,m;
	while(cin>>n>>m){
		int ans=0;
		vector<int> a,sum;
		a.push_back(0);
		for(int i=1;i<=n;i++){
			int s;
			cin>>s;
			a.push_back(s);
		}
		sort(a.begin(),a.end());
		for(int i=0;i<=n;i++){
			for(int j=i;j<=n;j++){
				if(a[i]+a[j]<=m)sum.push_back(a[i]+a[j]);
			}
		}
		sort(sum.begin(),sum.end());
		int suml=sum.size()-1;
		for(int i=0;i<=suml;i++){
			while(sum[i]+sum[suml]>m)suml--;
			if(ans<sum[i]+sum[suml])ans=sum[i]+sum[suml];
		}
		cout<<ans<<endl;
	}
}

C++: #include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<ctime>
using namespace std;
#define INF (1<<30)
typedef pair<int,int> Pa;
inline int mmin(int a,int b){ return a>b?b:a;}
inline int mmax(int a,int b){ return a>b?a:b;}
int N,M;
int P[1111];
struct Segment{
  Pa T[(1<<21)+1];
  int p[1001*1001+1];
  map<int,bool> f;
  int n;

  //節点の番号、追加する要素の区間[fi、se)
  void update(int k,int fi,int se){
    k=k+(n/2)-1;
    T[k].first=mmin(fi,T[k].first);
    T[k].second=mmax(se,T[k].second);
    while(k>0){//登りながら更新
      k=(k-1)/2;
      T[k].first=mmin(fi,T[k].first);
      T[k].second=mmax(se,T[k].second);
    }
  }

  void init(){//ツリー構築

    int ind=0;
    //P[i]+P[j]の足し算の結果を格納
    for(int i=0;i<N;i++){
      for(int j=0;j<N;j++){
	p[ind++]=P[i]+P[j];
      }
    }
    //最後にINFを付加
    p[N*N]=INF;

    //昇順ソート
    sort(p,p+N*N+1);
    unique(p,p+N*N+1);

    int _n;
    for(int i=0;i<N*N+1;i++){
      if(p[i]==INF){ _n=i+1; break;}
    }

    //nを2^mに
    n=1;
    while( n < _n*2 ) n*=2;
    
    //木を全て(INF~0)に初期化
    for(int i=0;i<n;i++){
      T[i]=Pa(INF,0);
    }

    //葉を追加
    for(int i=0;i<_n-1;i++){
      update(i,p[i],p[i+1]);
    }  
  }

  //値aより小さくもっと大きい数字は何か探す
  int search(int a){
    return query(0,a);
  }

  //区間[T[k].first,T[k].second)にaあるか否か
  int query(int k,int a){
    if(k>=n/2-1)
      return T[k].first;
    if(T[k].first<=a && a<T[k].second){
      int v=query(k*2+1,a);//左の葉
      if(v==-1)
	v=query(k*2+2,a);//右の葉
      return v;
    }
    return -1;
  }
  int size(){ return n;}
  void view(){
    printf("size=%d\n",this->size());
    int r=1;
    for(int i=0;i<n;i++){
      if(i==r){puts(""); r=(r+1)*2-1;}
      printf(" [%2d,%2d) ",T[i].first,T[i].second);
    }
  }

};

Segment S;

main(){

  /*   clock_t start,end;
       start=clock();//計測*/
  while(1){
    scanf("%d %d",&N,&M);
    if(!N && !M) break;
    for(int i=0;i<N;i++){
      scanf("%d",&P[i]);
    }
    P[N]=0;
    ++N;
    S.init();
    //    S.view();
    //    printf("%d=size\n",S.size());
    


    int res=0;
    for(int i=0;i<N;i++){
      for(int j=0;j<N;j++){
	int m=M-P[i]-P[j];
	if(m<0) continue;
	m-=S.search(m);
	res = max(res,M-m);
      }
    }
    /*   //時間を図り終える
	 end=clock();
	 printf("%.2f秒かかりました\n",(double)(end-start)/CLOCKS_PER_SEC);
    */

    printf("%d\n",res);
  }
}