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++: #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;


/**???????±?????°?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;
    vector<int> v,v2;
    while(1){
    cin >> n >> m;
    v.clear();v2.clear();
    if(n==0&&m==0)break;
    v.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--){
        int an=0;
        rep(j,0,v2.size()){
            if(i<j){
                a=true;
                break;
            }
            if(v2[i]+v2[j]>m)break;
            an=v2[i]+v2[j];
        //    cout << an << " a " << endl;
        }
        ans=max(ans,an);
        if(a)break;
    }
    cout << ans << endl;
    }
    return;
}

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

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
while(1){
int i,j,n,m,s=0,o;
static int p[1001],v[1001*1001]={0};
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
o=n+1;
for(i=1;i<=n;i++){
scanf("%d",&p[i]);
v[i]=p[i];
}
for(i=0;i<=n;i++){
for(j=i;j<=n;j++){
v[o]=p[i]+p[j];
o++;
}
}
sort(v,v+o);
for(i=0;i<o;i++){
for(j=i;j<o;j++){
if(v[i]+v[j]>m)break;
s=max(s,v[i]+v[j]);
}
}
printf("%d\n",s);
}
return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int N,M,P[1001];
vector<int> PP;
int main() {
	while(scanf("%d %d",&N,&M),N|M) {
		PP.clear();
		memset(P,0,sizeof(P));
		for(int i=1;i<=N;i++) scanf("%d",&P[i]);
		for(int i=0;i<N;i++) for(int j=0;j<N;j++) {
			PP.push_back(P[i]+P[j]);
		}
		sort(PP.begin(),PP.end());
		PP.erase(unique(PP.begin(),PP.end()),PP.end());
		int ans=0;
		for(int Qi=0;Qi<PP.size();Qi++) { //Qj<=M-Qi
			int lb=0,rb=PP.size();
			while(lb<rb) {
				int mid=(lb+rb)/2;
				if(PP[mid]>(M-PP[Qi])) rb=mid;
				else if(PP[mid]<(M-PP[Qi])) lb=mid+1;
				else break;
			}
			ans=max(ans,PP[Qi]+PP[rb-1]);
		}
		printf("%d\n",ans);
	}
}

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

int main(int argc, char *argv[]){
  int n, m;
  while(scanf("%d %d\n", &n, &m)){
    if(n == 0 && m == 0)break;
    int point[n + 1];
    point[0] = 0;
    set<int> a;
    for (int i = 1; i <= n; i++) {
      scanf("%d\n", &point[i]);
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a.insert(point[i] + point[j]);
      }
    }
    int ans = 0;
    for (int i: a){
      int tmp = *(--a.upper_bound(m - i));
      if(i + tmp <= m)
        ans = max(ans, i + tmp);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	vector<int> AB,P;
	vector<int>::iterator it;
	int maxpoint = 0,n,m,in,key,c;

	cin >> n >> m;
	while(n != 0 && m != 0){

	for(int i = 0; i < n; i++){
		cin >> in;
		P.push_back(in);
	}

	P.push_back(0);
	sort( P.begin(),P.end() );

	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= n; j++){
			AB.push_back(P[i]+P[j]);
		}
	}

	AB.push_back(m+1);
	sort( AB.begin(),AB.end() );

	for(int i = 0; i < AB.size() ; i++){
		key = m - AB[i];
		if(key < 0)	break;
		else	c = upper_bound(AB.begin(),AB.end(),key) - AB.begin() - 1;
		maxpoint = max(maxpoint,AB[i] + AB[c]);
	}

	cout << maxpoint << endl;
	
	maxpoint = 0;	
	AB.clear();
	P.clear();

	cin >> n >> m;
	}
	return 0;
}

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

#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int mato[501501];

int main(){
	while(1){
		int n,m;
		sintt(n,m);
		
		if(n==0)break;
		
		int mat[1001];
		mat[0]=0;
		rep(i,n){
			sint(mat[i+1]);
		}
		n++;
		int cn=0;
		rep(i,n){
			rep(j,n-i){
				mato[cn]=mat[j]+mat[j+i];
				cn++;
			}
		}
		
		sort(mato,mato+cn);
		
		/*
		rep(i,cn){
			printf("%d ",mato[i]);
		}puts("");
		*/
		int maxi=0;
		rep(i,cn){
			int *nn=lower_bound(mato,mato+cn,m-mato[i])-1;
			if(*nn+mato[i]>m){
				break;
			}
			maxi=max(maxi,mato[i]+*nn);
		}
		printf("%d\n",maxi);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(true){
		cin>>n>>m;
		if(n==0)	return 0;
		vector<int> p(n);
		for(int i=0;i<n;i++)	cin>>p[i];
		p.push_back(0);
		set<int> s;
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(auto it=s.begin();it!=s.end();it++){
			if(*it>=m)	break;
			auto it2=s.lower_bound(m-*it);
			if(it2!=s.begin())	it2--;
			ans=max(ans,*it+*it2);
		}
		cout<<ans<<endl;
	}
}