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 <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 1000000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

ll n,m;
vector<vector<bool>> dp(5,vector<bool>(2 * 1e8,0));

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(cin >> n >> m && n && m){
    vary(ll,s,1001,0);
    rep(i,n){
      cin >> s[i];
    }
    sort(s.begin(),s.begin()+n);
    dp = vector<vector<bool>>(5,vector<bool>(2 * 1e8,0));
    dp[0][0] = true;
    rep(i,4){
      rep(j,2 * 1e8+1){
        if(j > m) break;
        if(dp[i][j]){
          dp[i+1][j] = true;
          rep(k,4){
            if(j+s[k] <= m)
              dp[i+1][j+s[k]] = true;
          }
        }
      }
    }
    for(int i = m; i >= 0; --i){
      if(dp[4][i]){
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <map>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long lld;
typedef pair<lld, int> Pair;

int main( void )
{
	int n, m, input[4001];
	map<lld, int> field;
	while(cin >> n >> m, n | m) {
		field.clear();
		int tMax = 0;
		memset(input, 0, sizeof(input));
		for(int i=0; i < n; ++i) {
			cin >> input[i];
			for(int j=1; j <= 4; ++j) {
				lld tmp = input[i] * j;
				if(tmp > m || m < 0) {
					input[(j-1)*n + i] = 0;
					continue;
				}
				else
					input[(j-1)*n + i] = tmp;
				if(!field.count(tmp))
					field.insert(Pair(tmp, j));
				else
					field[tmp] = min(field[tmp], j);
				//cout << tmp << ' ';
			}
			//cout << endl;
			tMax = max(tMax, input[i]);
		}
		int sub = m;
		for(int i=1; i <= 4; ++i) {
			int nn = m - tMax * i;
			if(nn >= 0) sub = min(sub, nn);
		}
		n *= 4;
		input[n++] = 0;
		for(int i=n; i > 0; --i) {
			if(!input[i-1]) n = i+1;
		}
		//cout << "sub = " << sub << endl;
		field.insert(Pair(0, 0));
		sort(input, input+n, greater<int>());
		int Max = 0;
		for(int i=0; i < n; ++i) {
			for(int j=i; j < n; ++j) {
				int ii = input[i], jj = input[j];
				int count = field[ii] + field[jj];
				if(count <= 4) {
					int nn = m - ii + jj;
					int start = nn - sub, end = nn + sub + 1;
					for(int k=start; k < end; ++k) {
						if(k >= 0) {
							if(field.count(k)) {
								if(count + field[k] <= 4) {
									int tt = ii + jj + k;
									if(tt <= m) {
										Max = max(Max, tt);
										sub = m - Max;
									}
									//cout << ii << ' ' << jj << ' ' << k << ' ' << count << endl;
								}
							}
						}
					}
				}
			}
		}
		cout << Max << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef pair<int, bool> PIB;
typedef vector<int> VI;
typedef VI::iterator VII;

int n, m, ans = 0;
VI target, target2;

int main()
{
  while(true){
    scanf("%d%d", &n, &m);
    if(!n && !m)
      break;
    target.clear();
    target2.clear();
    ans = 0;

    target.push_back(0);
    for(int i = 0; i < n; i++){
      int p;
      scanf("%d", &p);
      target.push_back(p);
    }
    
    for(int i = 0; i < target.size(); i++)
      for(int j = 0; j < target.size(); j++)
	target2.push_back(target[i] + target[j]);
    
    sort(target2.begin(), target2.end());

    for(int i = 0; i < target2.size(); i++){
      VII p = upper_bound(target2.begin(), target2.end(), m - target2[i]);
      if(p != target2.begin())
	ans = max(ans, target2[i] + *(p - 1));
    }
    
    printf("%d\n", ans);
  }
  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;
/*
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 j=v2.size()-1;j>=0;j--){
        int an=0;
        for(int i=j-1;i>=0;i--){
            if(v2[i]+v2[j]<m){
                if(i==(j-1))a=true;
                ans=max(ans,v2[i]+v2[j]);
                break;
            }

        //    cout << an << " a " << endl;
        }
        if(a)break;
    }
    cout << ans << endl;
    }
    return;
}

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