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 _, val := range sums {
			rem := M - val
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, val+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

C++: #include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef long long INT;
typedef vector <INT> VI;
typedef set <INT> SI;

INT solve( VI Q, INT M ) {
    INT answer = 0;
    int n = Q.size();
    for ( int i = 0; i < n; ++ i ) {
        int q = Q[i];
        int L = 0;
        int R = n;
        INT m = M - q;
        while ( L < R ) {
            int p = ( R - L ) / 2 + L;
            INT t = q + Q[p];
            if ( t > M ) t = 0;
            if ( t == answer ) break;
            answer = max( answer, t );
            if ( Q[p] > m ) {
                R = p;
            } else {
                L = p + 1;
            }
        }
    }
    return answer;
}

int main() {
    int n, m;
    while ( cin >> n >> m ) {
        if ( n == 0 && m == 0 ) break;
        VI P;
        for ( int i = 0; i < n; ++ i ) {
            INT p;
            cin >> p;
            P.push_back( p );
        }
        P.push_back( 0 );
        VI Q;
        for ( int i = 0; i < n + 1; ++ i ) {
            for ( int j = 0; j < n + 1; ++ j ) {
                Q.push_back( P[i] + P[j] );
            }
        }
        sort( Q.begin(), Q.end() );
        cout << solve( Q, m ) << endl;
    }
    return 0;
}

C++: #include <iostream>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)

typedef unsigned long long ull;

ull dp[1000][5];

int main(){
    int N;
    ull M, P[1001];

    while(std::cin >> N >> M, N){
        REP(i, N){
            std::cin >> P[i];
        }

        REP(i, 1000){
            REP(j, 5){
                dp[i][j] = 0;
            }
        }

        REP(i, 5){
            if(P[0] * i <= M){
                dp[0][i] = P[0] * i;
            }
        }

        FOR(i, 1, N){
            REP(j, 5){
                REP(k, j+1){
                    if(P[i] * k + dp[i-1][j-k] <= M){
                        dp[i][j] = std::max(P[i] * k + dp[i-1][j-k], dp[i][j]);
                    }
                }
            }
        }

        ull res = 0;
        REP(i, 5){
            res = std::max(dp[N-1][i], res);
        }

        std::cout << res << std::endl;
    }

    return 0;
}

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

using namespace std;

int N;
int M;
long long ans=0;

/*
int binarySearch(int t){
    int lo=0,hi=N-1;
    int te;
    while(true){
        int m=(lo+hi)/2;
        if(P[m]<t){
            hi=(m-1)<=1?0:m-1;
        }else if(P[m]>t){
            lo=m+1;
        }
        if(lo>=hi){
            te=hi;
        }
    }
    return te;
}*/
/*
void func(int dart,long long sum,long long index){
    ans=max(ans,sum);
    if(dart==4)return ;
    long long s=M-sum;
    auto itF = (P.begin()+index);
//    auto itF = P.begin();
//    advance(itF,index);
    
    auto it = upper_bound(P.begin(),itF,s);
    it = prev(it);

    
    for(auto i=it;i!=P.begin();i--){
        func(dart+1,sum+*i,distance(P.begin(),i));
    }
    
 
    if(sum+P[n]<=M){
        func(n-1,dart+1,sum+P[n]);
        func(n,1+dart,sum+P[n]);
        func(n-1,dart,sum);
    }
    else {
        func(n-1,dart,sum);
    }
 
    
    return ;
}
*/

vector<long long>P;
int main(){
    while(cin>>N>>M,N,M){
        P = vector<long long>(N+1,0);
        for(int i=0;i<N;i++){
            cin>>P[i];
        }
        
        vector<long long> v;
        for(int i=0;i<N+1;i++){
            for(int j=0;j<N+1;j++){
                if(P[i]+P[j]<=M)v.push_back(P[i]+P[j]);
            }
        }
        sort(v.begin(),v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        for(int i=0;i<v.size();i++){
            long long ind = upper_bound(v.begin(),v.end(),M-v[i])-v.begin();
            ans = max(ans,v[i]+v[ind-1]);
        }
        cout<<ans<<endl;
        ans=0;
    }
    return 0;
    
}
//574 999424

C++: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL<<61;

int p[1005], n, m, nn;
vector<int> gedo;

inline void cut(int *k) {
  if (m < *k) *k = 0;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> n >> m, n) {
    gedo.clear();
    for (int i = 0; i < n; ++i) {
      cin >> p[i];
    }

    for (int i = 0; i < n; ++i) {
      for (int j = i; j < n; ++j) {
        gedo.PB(p[i] + p[j]);
      }

      gedo.PB(p[i]);
    }
    gedo.PB(0);
    sort(gedo.begin(), gedo.end());
    gedo.PB(INF);

    int ans = 0;
    for (int i = 0; i < gedo.size() - 1; ++i) {
      int l = 0, r = gedo.size() - 1, cur = gedo[i];

      while (1 <= r - l) {
        int j = (r + l) / 2, temp1 = cur + gedo[j], temp2 = cur + gedo[j + 1];
        cut(&temp1);
        cut(&temp2);
        ans = max(ans, temp1);

        if (temp1 != 0 && temp2 == 0) break;

        if (temp1 == 0) {
          r = j;
        } else {
          l = j + 1;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

int main(){
while(1){
	int n,m,a,ans=0;
	vector<int> p,pp;
	scanf("%d%d",&n,&m);
	p.push_back(0);
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		p.push_back(a);
	}
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			pp.push_back(p[i]+p[j]);
		}
	}
	sort(pp.begin(),pp.end());
	int s=pp.size()-1;
	for(int i=0;i<=s;i++){
		while(pp[i]+pp[s]>m)s--;
		if(ans<pp[i]+pp[s])ans=pp[i]+pp[s];
	}
	printf("%d\n",ans);
}
}