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>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;


signed main() {
	
	while(1) {
		cin2(n,m);
		if (n + m == 0) break;

		vi p(n,0);
		rep(i,0,n) {
			cin1(p[i]);
		}
		p.pb(0);

		rep(i,0,n) {
			rep(j,0,n) {
				if (p[i] + p[j] <= m) { 
					p.pb(p[i] + p[j]);	
				}
			}
		}
		sort(p.begin(),p.end());

		int size = p.size();

		int ans = 0;
		rep(i,0,size) {
			int c_d = p[i];
			int a_b = *(upper_bound(p.begin(),p.end(),m - c_d) - 1);
			ans = max(ans,a_b + c_d);
		}
		cout(ans);
	}



	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

int N, M;
int P[1000];
vector<int> D;

int main() {
  while (cin >> N >> M) {
    if (N == 0 && M == 0) break;
    for (int i=0; i<N; i++) cin >> P[i];

    D.clear();
    D.push_back(0);
    for (int i=0; i<N; i++) {
      D.push_back(P[i]);
      for (int j=0; j<N; j++) {
        D.push_back(P[i] + P[j]);
      }
    }

    sort(D.begin(), D.end());
    int ans = 0;
    for (auto x : D) {
      int t = x + *(upper_bound(D.begin(), D.end(), M - x) - 1);
      if (t <= M) ans = max(ans, t);
    }
    cout << ans << "\n";
  }
  return 0;
}

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){
      ans = max(ans, i + *(--a.upper_bound(m - i)));
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
  
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
  
using namespace std;
  
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
  
const int INF=1<<29;
const double EPS=1e-9;
  
const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
    int N, T;
    cin >> N >> T;
    vector<int> P(N);
    for (int i = 0; i < N; i++) {
        cin >> P[i];
    }

    vector<int> PP;
    PP.push_back(0);
    for (int i = 0; i < N; i++) {
        PP.push_back(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());

    int ans = 0;
    for (int i = 0; i < PP.size(); i++) {
        int k = upper_bound(PP.begin(), PP.end(), T - PP[i]) - PP.begin();
        if (k != 0) {
            ans = max(ans, PP[i] + PP[k - 1]);
        }
    }
    cout << ans << endl;
    return 0;
}

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);
			while(it2!=s.begin()&&*it2+*it>=m)	it2--;
			if(*it+*it2<m)	ans=max(ans,*it+*it2);
		}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>//JOI 2007 ??¬??? 3??????
using namespace std;
int max_p[200000001];
int p[1001];
int main ()
{
	long n,m;
	long i,j;
	int r;
	while(cin >> n >> m,n)
	{
		for(i=0;i<=m;i++)
		{
			max_p[i]=0;
		}
		for(i=0;i<n;i++)
		{
			cin >> p[i];
		}
		max_p[0]=1;
		for(i=0;i<m;i++)
		{
			if(max_p[i]>=1 && max_p[i]<5)
			{
				r=max_p[i];
				for(j=0;j<n;j++)
				{
					if(i+p[j]<=m)
					{
						if(max_p[i+p[j]]==0 || r+1<max_p[i+p[j]])
						{
							max_p[i+p[j]]=r+1;
						}
					}
				}
			}
		}
		for(i=m;i>=0;i--)
		{
			if(max_p[i]!=0)
			{
				cout <<  i << endl;
				break;
			}
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> plli;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>=0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()

const ll mod = 1e9 + 7;
const ll INF = 1<<30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n, m;

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    while (n != 0) {
        int p[1005] = {};
        rep(i, n) cin >> p[i];
        vector<int> score;
        score.push_back(0);
        score.push_back(1e12);
        rep(i, n) score.push_back(p[i]);
        rep(i, n) rep(j, n) {
            score.push_back(p[i] + p[j]);
        }
        sort(all(score));
        score.erase(unique(all(score)), score.end());
        int ans = 0;
        rep(i, score.size()) {
            if (score[i] > m) break;
            int l = 0;
            int r = score.size() + 1;
            while (r - l > 1) {
                int mid = (r + l) / 2;
                if (score[i] + score[mid] <= m) l = mid;
                else r = mid;
            }
            int idx = l;
            if (score[i] + score[idx] > m) continue;
            ans = max(ans, score[i] + score[idx]);
        }
        cout << ans << endl;
        cin >> n >> m;
    }
}