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++: //Solution for AOJ 0529: Darts
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 1000, MAX_M = 200000000;
int n, m;
int p[MAX_N + 1], p2[MAX_N*MAX_N], ans, roop, s, mid;
void search(int end, int start){
	if ((end + start) % 2)
		mid = (end + start) / 2 + 1;
	else
		mid = (end + start) / 2;
	if (p2[mid] == s)
		return;
	if (end == start){
		if (p2[mid] > s)
			mid--;
		return;
	}
	if (p2[mid] < s){
		if (end - mid == 1){
			if (p2[end]<s)
				mid++;
			return;
		}
		search(end, mid);
	}
	else{
		if (mid - start == 1){
			mid--;
			return;
		}
		search(mid, start);
	}
}
int main(){
	while (cin >> n >> m){
		ans = 0;
		for (int i = 1; i <= n + 1; i++){
			cin >> p[i];
		}
		roop = 0;
		for (int i = 0; i <= n + 1; i++){
			for (int j = i; j <= n + 1; j++){
				if (p[i] + p[j] <= m){
					p2[roop] = p[i] + p[j];
					roop++;
				}
			}
		}
		sort(p2, p2 + roop);
		for (int i = 0; i < roop; i++){
			s = m - p2[i];
			search(roop - 1, 0);
			ans = max(ans, p2[i] + p2[mid]);
		}
		cout << ans << endl;
		return 0;
	}
}

C++: //JOI 2008??´???3??? ?????????

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>

using namespace std;

#define MAX -210000000
#define MIN 210000000

int a,b;
int d[1000],c[1000*1000+2],hk=0,mcunt,low,high;
int x,pr=0,flg[200000000],m,ff=0;

int main(){ 
	
	scanf("%d %d",&a,&b);
	
	d[0]=0;
	hk=0;
	for(int i=1;i<=a;i++) scanf("%d",&d[i]);
	
	for(int i=0;i<=a;i++){
		for(int j=0;j<=a;j++){
			if(flg[d[i]+d[j]]!=1){
				c[hk]=d[i]+d[j];
				//printf("%lld\n",c[cunt]);
				hk++;
				flg[d[i]+d[j]]=1;
			
			}
		}
	}
	//printf("Sort???\n");
	sort(c,c+hk);
	/*printf("\n");
	for(int i=0;i<hk;i++) printf("%d\n",c[i]);
	printf("\n");
	
	printf("hk=%d\n",hk);
	printf("c[low]=%d c[high]=%d\n",c[hk],c[hk-1]);*/
	                                 
	for(int i=0;i<hk;i++){
		x=b-c[i];
		//printf("x=%d\n",x);
		low=0;
		high=hk-1;
		
		while(low<=high){
			m=(low+high)/2;
			if(x==c[m]){
				ff=1;
				break;
			}
			if(x>c[m]) low=m+1;
			else if(x<c[m]) high=m-1;
		}
		//printf("\n%d\n",low);
		if(ff==1){
			pr=c[i]+c[m];
			break;
		}
		if(b>c[i]+c[high] && pr<c[i]+c[high]){
			pr=c[i]+c[high];
		//	printf("%d %d\n",c[i],c[high]);
		}
	}
	printf("%d\n",pr);
	return 0;
}

C++: #include<stdio.h>
#include<stdio.h>
#include<bits/stdc++.h>
#include<vector>
#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
using namespace std;

vector<int> v,v2;

struct a{

};

/**???????±?????°?4??????????\????**/

void a1(){
    int i, len = 3;
    char str[] = "abc";
    for(i=0;i<len;i++){
        printf("%c\n",str[2-i]);
    }
    return;
}

int length(char str[5]){
    int cut=0;
    while(*(str+cut)!='\0'){
        cut++;
    }
    return cut;
}

void a2(){
    char a[4][5]={"ab","cdef","g","hij"};
    int i;
    for(i=0; i<4; i++){
        printf("%d\n",length(a[i]));
    }
    return;
}

void a3(){
    char str[4];
    strcpy(str,"abc");
    printf("%s\n",str);
    return;
}

void a4(){
    char a[7]="saitou",b[7] = "suzuki";
    int i;

    for(i=0;i<7;i++){
        int k = a[i]-b[i];
        if(k > 0){
            printf("%c",a[i]);
        }else if(k < 0){
            printf("%c",b[i]);
        }else{
            printf("0");
        }
    }
    printf("\n");
    return;
}

void a5(){
    char *a[3];
    int n,i,j,cut = 0;
    for(i=0;i<3;i++){
        scanf("%d",&n);
        a[i]=(char *)malloc((n+1)*sizeof(char));
        scanf("%s",a[i]);
    }
    for(i=0;i<3;i++){
        for(j=0;j<strlen(a[i]);j++){
            if(a[i][j]=='P'||a[i][j]=='p'){
                cut++;
            }
        }
    }
    printf(">> %d\n",cut);
    for(i=0;i<3;i++){
        free(a[i]);
    }
}

void y(){
    int n,m;
    cin >> n >> m;
    v2.push_back(0);
    rep(i,0,n){
        int x;cin >> x;
        v.push_back(x);
    }
    rep(i,0,v.size()){
        rep(j,0,v.size()){
            v2.push_back(v[i]+v[j]);
        }
    }
    sort(v2.begin(),v2.end());
    int ans=0;
    //cout << v2.size() << " a " <<  endl;
    bool a=false;
    for(int i=v2.size()-1;i>=0;i--){
        rep(j,0,v2.size()){
            if(v2[i]+v2[j]>m)break;
            if(i<j){
                a=true;
                break;
            }
           // cout << "a";
            ans=max(ans,v2[i]+v2[j]);
        }
        if(a)break;
    }
    cout << ans << endl;
    return;
}

signed main(){
    y();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int n, m;
int main()
{
	while (cin >> n >> m, n)
	{
		vector<int> v, w;
		REP(i, n)
		{
			int a;
			cin >> a;
			w.push_back(a);
		}
		w.push_back(0);
		set<int> used;
		REP(i, w.size())REP(j, w.size())
		{
			int t = w[i] + w[j];
			if (used.find(t) == end(used))
			{
				used.insert(t);
				v.push_back(w[i] + w[j]);
			}
		}
		sort(ALL(v));
		int ans = 0;
		int i = 0;
		int j = v.size() - 1;
		while (i<=j)
		{
			if (v[i] + v[j] <= m)
			{
				ans = max(ans, v[i] + v[j]);
				i++;
			}
			else
			{
				j--;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;
 
int32_t N, M, P[1123];
std::set<int32_t>list;
int32_t ans;
 
int main()
{
    while (true)
    {
        N = M = 0;
        memset(P, 0, sizeof(P));
        list.clear();
        ans = 0;
 
        in >> N >> M;
        if (N == 0 && M == 0) { break; }
        rep(i, N) { in >> P[i]; }
 
        P[N] = 0;
        N++;
        //std::sort(P, P + N);
        rep(i, N)
        {
            rep(j, N) { list.insert(P[i] + P[j]); }
        }
        for (auto i : list)
        {
            if (i > M) { continue; }
            auto iter = list.upper_bound(M - i);
            --iter;
            ans = std::max(ans, i + *iter);
        }
 
        out << ans << std::endl;
    }
    return 0;
}