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<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(v2[i]+v2[j]>m)break;an=v2[i]+v2[j];
            if(i<j){
                a=true;
                break;
            }
        //    cout << an << " a " << endl;
        }
        ans=max(ans,an);
    }
    cout << ans << endl;
    }
    return;
}

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

C++: #include <cstdio>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n, m, c[1009], s[510009], t[510009], cnt[1027], sum[1027];
int main() {
	while (scanf("%d%d", &n, &m), n) {
		for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
		sort(c, c + n);
		int b = 0;
		for (int i = 0; i <= n; i++) {
			for (int j = i; j <= n && c[i] + c[j] <= m; j++) {
				s[b++] = c[i] + c[j];
			}
		}
		for (int i = 0; i < 30; i += 15) {
			fill(cnt, cnt + 32768, 0); sum[0] = 0;
			for (int j = 0; j < b; j++) cnt[(s[j] >> i) & 32768]++;
			for (int j = 0; j < 32768; j++) sum[j + 1] = sum[j] + cnt[j];
			for (int j = 0; j < b; j++) t[sum[(s[j] >> i) & 32768]++] = s[j];
			for (int j = 0; j < b; j++) s[j] = t[j];
		}
		int r = b - 1, ret = 0;
		for (int i = 0; i < b && i <= r; i++) {
			while (r > 0 && s[i] + s[r] > m) r--;
			ret = max(ret, s[i] + s[r]);
		}
		printf("%d\n", ret);
	}
	return 0;
}

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

#define MAX_N 1001

using namespace std;

int main(void){
	while( 1 ){
		int n, m;
		int V[MAX_N] = {0,};
		int D[MAX_N][4] = {{0,},};
		int Max = 0;

		cin >> n >> m;

		if( n == 0 && m == 0 ){
			break;
		}

		for(int i = 0; i < n; i++){
			cin >> V[i];
		}
		V[n] = 0;
		n++;

		sort(V, V + n);
		reverse(V, V + n);

		for(int i = 0; i < n; i++){
			for(int j = 0; j < 4; j++){
				D[i][j] = V[i] > m ? -1 : V[i];
				if( j > 0 ){
					int thisMax = 0;

					if( D[i][j - 1] < 0 ){
						D[i][j] = -1;
						continue;
					}
					for(int k = 0; k <= i; k++){
						if( D[k][j - 1] < 0 ){
							continue;
						}
						int tmp = D[i][j] + D[k][j - 1];
						thisMax = max(thisMax, tmp);
					}
					if( thisMax > D[i][j] ){
						D[i][j] = thisMax > m ? -1 : thisMax;
					}
					
				}
			}
		}

		for(int i = 0; i < n; i++){
			for(int j = 0; j < 4; j++){
				//cout << D[i][j] << ' ';
				if( D[i][j] <= m ){
					if( Max < D[i][j] ){
						Max = D[i][j];
					}
				}
			}
			//cout << '\n';
		}

		cout << Max << '\n';
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001


int main(){
  LL n,m,p[1000];
  while(true){
    cin >> n >> m;
    if(n==0 and m==0)break;
    for(LL i=0;i<n;i++){
      cin >> p[i];
    }
    sort(p,p+n);
    LL res,res1,res2,res3,res4;
    res = res1 = res2 = res3 = res4 = 0;
    for(LL i=0;i<n;i++){
      if(p[i] > m)break;
      res1 = p[i];
    }
    res = max(res,res1);
    LL p2[1000000];
    for(LL i=0;i<n;i++){
      for(LL j= 0;j<n;j++){
        p2[i*n+j] = p[i]+p[j];
      }
    }
    LL p2num = n*n;
    sort(p2,p2+p2num);
    res2 = p2[upper_bound(p2,p2+p2num,m)-p2-1];
    res = max(res,res2);
    for(LL i=0;i<n;i++){
      if(p[i] > m)continue;
      res3 = max(res3,p[i] + p2[upper_bound(p2,p2+p2num,m-p[i]) - p2 -1]);
    }
    res = max(res,res3);
    for(LL i=0;i<p2num;i++){
      if(p2[i] > m)continue;
      res4 = max(res4,p2[i] + p2[upper_bound(p2,p2+p2num,m-p2[i]) - p2 -1]);
    }
    res = max(res,res4);
    cout << res << endl;

  }
	return 0;
}

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

int N, M;
vector<int> point;

int main() {
	while (cin >> N >> M, N || M) {
		point.clear(); point.resize(N);
		for (int i = 0; i < N; i++) cin >> point[i];

		set<int> pos;
		pos.insert(0);
		for (int i = 0; i < N; i++) pos.insert(point[i]);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (point[i] + point[j] <= M) {
					pos.insert(point[i] + point[j]);
				}
			}
		}

		int max_point = 0;
		for (set<int>::iterator pos_i = pos.begin();
				pos_i != pos.end(); pos_i++) {
			for (set<int>::iterator pos_j = pos.begin();
					pos_j != pos.end(); pos_j++) {
				if (*pos_i + *pos_j <= M) max_point = max(max_point, *pos_i + *pos_j);
			}
		}

		cout << max_point << endl;
	}
	return 0;
}