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++: //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;
}

C++: 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	int ans[4];
	int temp;
	double energy,energy_min,energy_now;
	int M, N;
	int* p;
	int i, j;
	double T=100;
	int ret;
	while (1) {
		scanf("%d%d", &N, &M);
		p = (int*)malloc((N+1) * sizeof(int));
		for (i = 0; i < N; i++) {
			scanf("%d", &p[i]);
		}
		p[N] = 0;
		if (N == M && N == 0) {
			break;
		}
		energy_min = pow(10, 300);
		for (j = 0; j < 4; j++) {
			ans[j] = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
		}
		
		for (T = 1000000; T > 1000; T--) {
			for (j = 0; j < 4; j++) {
				temp = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
				for (j = 0; j < 4; j++) {
					ret = ans[0] + ans[1] + ans[2] + ans[3];
					ret += temp - ans[j];
					energy = M - ret;
					if (M < ret)energy += 100000000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*10)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}