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++: #include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(void)
{
   int n,m,input,s;
   vector<int>a;
   while(scanf("%d%d",&n,&m) ,n,m)
   {
      a.clear();
      a.push_back(0);
      for(int i = 0;i < n;i++)
      {
         scanf("%d",&input);
         a.push_back(input);
      }

      s = a.size();
      for(int i = 0;i < s;i++)
         for(int j = 0;j < s;j++)
            if(a[i] + a[j] <= m) a.push_back(a[i]+a[j]);

      s = a.size();
      for(int i = 0;i < s;i++)
      {
         for(int j = 0;j < s;j++)
            if(a[i]+a[j] < m) a.push_back(a[i]+a[j]);
      }

      s = a.size();
      int most = 0;
      for(int i = 0;i < s;i++)
      {
         if(most < a[i]) most = a[i];
      }

      printf("%d\n",most);
   }


   return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define INF (2000000000)
#define MOD (1000000007)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;
typedef vector<long> VL;
typedef vector<long>::iterator VLI;
typedef pair<int, int> P;

long a[1001];
VL score;

int main()
{
	int N;
	long M;
	
	while (scanf("%d %ld", &N, &M), N + M){
		for (int i = 1; i <= N; i++){
			scanf("%ld", &a[i]);
		}
		for (int i = 0; i <= N; i++){
			for (int j = 0; j <= N; j++){
				score.push_back(a[i] + a[j]);
			}
		}
		sort(score.begin(), score.end());
		
		int size = score.size();
		long maxi = 0;
		int l = 0;
		int r = size - 1;
		while (l < r){	
			if (score[l] + score[r] <= M){
				maxi = max(maxi, score.at(l) + score.at(r));
				l++;
			}
			else {
				r--;
			}
		}
		
		printf("%ld\n", maxi);
		score.clear();
	}
	
	return 0;
}

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