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 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];
		sort(ALL(ps2));		
		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;
vector<long int> po;
vector <pair <int ,long int> > sss;
long int n,a,low,m,lastans,memo[1005];
void solve(int dp,long int sum);

int main(){
    while(1){
        lastans=0;
        po.clear();
        //for(int i=0;i<1005;i++)memo[i]=0;

        cin >> n >> m;
        if(n==0&&m==0)break;
        for(int i=0;i<n;i++){
            cin >> a;
            po.push_back(a);
           // low=min(low,a);
        }
        sort(po.begin(),po.end());
        //cout << po[0] << endl;
        //solve(0,0);
        //lastans=po[n-1];

        for(int i=4;i>0;i--){
            for(int j=n-1;j>=0;j--){
                if(m>=i*po[j]){
                    lastans=max(i*po[j],lastans);

                    for(int k=4-i;i>0;i--){
                        for(int s=0;s<n;s++){
                            if(m>=i*po[j]+po[s]){
                                lastans=max(i*po[j]+po[s],lastans);

                                for(int t=4-k;t>0;t--){
                                    for(int u=0;u<n;u++){
                                        if(m>=i*po[j]+po[s]+po[u]){
                                         lastans=max(i*po[j]+po[s]+po[u],lastans);

                                         for(int v=4-t;v>0;v--){
                                            for(int w=u-1;w>=0;w--){
                                                if(m>=i*po[j]+po[s]+po[u]+po[w]){
                                                    lastans=max(lastans,i*po[j]+po[s]+po[u]+po[w]);
                                                    break;
                                                }else break;
                                            }
                                         }
                                            //break;
                                        }else break;
                                    }
                                }
                                //break;
                            }else break;

                        }
                    }

                    //lastans=max(lastans,po[j]*i);
                    //break;
                }else break;
            }
        }



        cout << lastans << endl;
        //cout <<"ss=" << ss << endl;
    }

    return 0;
}

C++: #include<iostream>
using namespace std;
int p[1001],s[1000000];
int main(){
	int n,m;
	cin>>n>>m;
	int top=1;
	p[0]=0,s[0]=0;
	for(int i=1;i<=n;i++){
		cin>>p[i];
		for(int j=0;j<i;j++){
			if(p[i]+p[j]<=m){s[top]=p[i]+p[j]top++;}
		}
	}
	int ans=0;
	for(int i=0;i<top;i++){
		int left=0,right=top,mid;
		while(true){
			if(left>=right-1)break;
			mid=(left+right)/2;
			if(s[mid]+s[i]<=m){ans=max(ans,s[mid]+s[i]),left=mid;}
			else right=mid;
		}
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include<stdio.h>
#include<algorithm>
int i,j;
int gou[6000000];
using namespace std;
int main(void){
	int n,m;
	int data[1001];
	scanf("%d %d",&n,&m);
	while(n!=0 && m!=0){
		for(i=0;i<n;i++){
			scanf("%d",&data[i]);
		}
		data[n]=0;
		sort(data,data+n+1);
		/*for(i=0;i<=n;i++){
			printf("%d \n",data[i]);
		}*/
		int cnt=0;
		for(i=0;i<=n;i++){
			for(j=i;j<=n;j++){
				gou[cnt]=data[i]+data[j];
				cnt++;
			}
		}
		sort(gou,gou+cnt);
		/*for(i=0;i<cnt;i++){
			printf("%d ",gou[i]);
		}
		printf("\n");*/
		//
		int max=0;
		for(i=0;i<cnt;i++){
			int syou=0,dai=cnt,sach;
			sach=m-gou[i];
			while(dai>=syou){
				int b;
				b=(dai+syou)/2;
				if(sach==gou[b]){
					max=m;
					break;
				}
				else if(sach>gou[b]){
					syou=b+1;
				}
				else if(sach<gou[b]){
					dai=b-1;
				}
			}
			if(max<gou[dai]+gou[i] && m>=gou[dai]+gou[i]){
				max=gou[dai]+gou[i];
			}
		}
		printf("%d\n",max);
		scanf("%d %d",&n,&m);
	}
	return 0;
}