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<algorithm>
#include<vector>

#define MAX_N 1001

using namespace std;

class CNum{
public:
	vector<int> v;
};

bool Find(vector<int> &v, int search){
	for(unsigned int i = 0; i < v.size(); i++){
		if( v[i] == search ){
			return true;
		}
	}
	return false;
}

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

		cin >> n >> m;

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

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

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

		for(int i = 0; i < n; i++){
			for(int j = 0; j < 4; j++){
				if( j == 0 ){
					D[j].v.push_back( V[i] );
				}else if( j > 0 ){
					D[j].v.clear();
					for(unsigned int l = 0; l < D[j - 1].v.size(); l++ ){
						int tmp = V[i] + D[j - 1].v[l];
						if( tmp <= m ){
							int cand = 0;
							int index = -1;

							if( Max < tmp ){
								Max = tmp;
							}
							for(int k = 0; k < n; k++){
								cand = tmp + V[k];
								if( cand <= m ){
									index = k;
									break;
								}
							}
							if( index >= 0 && cand >= Max && !Find(D[j].v, tmp) ){
								Max = cand;
								D[j].v.push_back( tmp );
							}
						}
					}
				}
			}
		}

		for(int i = 0; i < 4; i++){
			//cout << " " << j << ":" << '\n';
			for(unsigned int j = 0; j < D[i].v.size(); j++ ){
				//cout << D[j].v[k] << ' ';
				int tmp = D[i].v[j];
				if( tmp <= m && Max < tmp){
					Max = tmp;
				}
			}
			//cout << '\n';
		}
		cout << Max << '\n';
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(int i=a; i<int(b); i++)
#define rep(i,b) loop(i,0,b)
#define each(e,c) for(auto& e:c)
#define iter(it,c) for(auto it=c.begin(); it!=c.end(); ++it)

ll const inf = 1<<28;
double const pi = acos(-1);

int a[1024];
int d[2048];

int solve(int n, int k){
    int c=0;
    rep(i,n)loop(j,i,n){
        int s=a[i]+a[j];
        if(s<=k){
            d[c++]=s;
        }
    }
    sort(d,d+c);
    int ans=0;

    rep(i,c){
        int s1=d[i];
        int lo=0, hi=c;
        while(lo+1<hi){
            int mid=(lo+hi)>>1;
            int s2=d[mid];
            if(s1+s2>k){
                hi=mid;
            }else{
                lo=mid;
            }
        }
        int s2=d[lo];
        ans=max(ans, s1+s2);
    }
    return ans;
}

int main(){
    int n,k;
    while(cin>>n>>k && n|k){
        a[0]=0;
        rep(i,n)cin>>a[i+1];
        n++;
        cout << solve(n,k) << endl;
    }
}

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

int main() {
	while(true){
	int N,M;
	int num[1001];
	vector<int> nums;
	cin >> N >> M;
	if(N==0)
	break;
	num[0]=0;
	for(int i=1;i<=N;i++){
		cin >> num[i];
	}
	for(int i=0;i<=N;i++){
		for(int j=0;j<=N;j++){
			if(i>=j){
			nums.push_back(num[i]+num[j]);
			
			}
		}
	}
	sort(nums.begin(),nums.end());
	int now=nums.size()-1,maxer=0;
	for(int i=0;i<nums.size();i++){
		if(nums[i]<=M){
	    while(nums[now]+nums[i]>M && now!=0)
		now--;
		
		maxer=max(nums[now]+nums[i],maxer);
		}
	}
	cout << maxer << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#define fi "darts.inp"
#define fo "darts.out"
using namespace std;
long long n,m,a[100011],ans;
vector<long long > vt;
int main()
{
//    freopen (fi , "r" ,stdin);
//    freopen (fo , "w" ,stdout);
    while (1)
    {
    scanf("%lld%lld",&n,&m);
    if (n==0&&m==0) return 0;
    for (int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    n++;
    a[n]=0;
    vt.clear();
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            vt.push_back(a[i]+a[j]);
        }
    sort(vt.begin(),vt.end());
    int c;
    c=vt.size()-1;
    for (int i=0;i<vt.size();i++)
    {
        while (c>=0&&vt[c]+vt[i]>m)
            c--;
        if (vt[c]+vt[i]<=m) ans=max(ans,vt[c]+vt[i]);
    }
    cout<<ans<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589


#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*) array, (T*) (array + N), val);
}
pii Dir[8] = { //?§????
	{ 0 ,1 },{ -1 ,0 },{ 1 ,0 },{ 0 ,-1 },
	{ 1 ,1 },{ 1 ,-1 },{ -1 ,1 },{ -1 ,-1 }
};

signed main() {
	int n, m;

	scanf("%d %d", &n, &m);
	vector<int> po(n);
	REP(i, n) {
		scnaf("%d", &po[i]);
	}
	po.push_back(0);
	set<int> toku;
	REP(i, n+1) {
		REP(j, n+1) {
			toku.insert(po[i]+po[j]);
		}
	}
	int ans = 0;
	for (auto itr : toku) {
		auto itr2 = --toku.upper_bound(m-itr);
		if(itr2 != toku.end())
			ans = max(itr+*itr2, ans);
	}
	printf("%d\n",ans);
	return 0;
}

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

int N,M;
int P[1001];
int S;

int arara(){
    for(int i = 0; i <= N; i++){
        for(int j = 0; j <= N; j++){
            for(int k = 0; k <= N; k++){
                for(int l = 0; l <= N; l++){
                    if(S <= P[i] + P[j] + P[k] + P[l]&&P[i] + P[j] + P[k] + P[l] <= M){
                        S = P[i] + P[j] + P[k] + P[l];
                    }
                }
            }
        }
    }
    return S;
}

int main(){
    P[0] = 0;
    for(;;){
        if(cin >> N >> M){
            for(int i = 1; i < N + 1; i++){
                cin >> P[i];
            }
            cout << arara() << endl;
        }
    }
    return 0;
}