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

using namespace std;

int N, M;
int a[1005];
vector<int> vec;

int main(void)
{
	while(1){
		cin >> N >> M;
		if(N == 0 && M == 0) break;
		for(int i = 0; i < N; i++) cin >> a[i];
		a[N] = 0;
		
		vec.clear();
		for(int i = 0; i <= N; i++){
			for(int j = 0; j <= N; j++){
				vec.push_back(a[i] + a[j]);
			}
		}
		sort(vec.begin(), vec.end());
		
		int ans = 0;
		for(int i = 0; i < vec.size(); i++){
			auto p = upper_bound(vec.begin(), vec.end(), M-vec[i]);
			if(p == vec.begin()) continue;
			ans = max(ans, vec[i] + *(--p));
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n, m, c[1009], s[1000009];
int main() {
	while (scanf("%d%d", &n, &m), n) {
		for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
		sort(c, c + n);
		int b = 0;
		for (int i = 0; i <= n; i++) {
			for (int j = i; j <= n && c[i] + c[j] <= m; j++) {
				s[b++] = c[i] + c[j];
			}
		}
		sort(s, s + b);
		int r = b - 1, ret = 0;
		for (int i = 0; i < b && i <= r; i++) {
			while (r > 0 && s[i] + s[r] > m) r--;
			ret = max(ret, s[i] + s[r]);
		}
		printf("%d\n", ret);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <cstdio>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

int n;
llong m;

llong bs(vector<llong> v, llong t){
	int l=0, r=v.size()-1;
	int mid;
	while(l<r){
		mid=(l+r)/2;
		if(v[mid]>t) r=mid-1;
		else if(v[mid]==t) return v[mid];
		else{
			if(v[mid+1]>t) return v[mid];
			else l=mid+1;
		}
	}
	
	return v[l];
}

llong solve(vector<llong> p){
	vector<llong> v(n*n);
	int c=0;
	REP(i, n){
		REPR(j, n-1){
			v[c]=p[i]+p[j];
			c++;
		}
	}
	VSORT(v);
	
	llong res=0;
	REP(i, c){
		if(v[i]>m) continue;
		res=max(res, v[i]+bs(v, m-v[i]));
	}
	return res;
}

int main(){
	while(1){
		cin >> n >> m;
		if(n==0 && m==0) return 0;
		n++;
		vector<llong> p(n);
		p[0]=0;
		FOR(i, 1, n) cin >> p[i];
		VSORT(p);
		cout << solve(p) << endl;	
	}
}

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

using namespace std;

int main()
{
    int N, M;
    while (cin >> N >> M, N) {
        vector<int> a(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                a.push_back(a[i] + a[j]);
            }
        }

        sort(a.begin(), a.end());
        
        N = a.size();
        
        int res = 0;
        for (int i = 0; i < N; i++) {
            int m = M - a[i];
            if (m < 0) break;
            
            int l = 0, r = N;
            while (r - l > 1) {
                int mid = (l + r) / 2;
                if (a[mid] < m) {
                    l = mid;
                } else {
                    r = mid;
                }
            }
            res = max(res, a[i] + a[l]);
        }
        cout << res << endl;
    }
    return 0;
}

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

void solve(){
    ll n,m;
    ll ans;

    while(cin>>n>>m,n|m){
        vector<ll> v,sum_v;

        for(ll i=0;i<n;i++){
            ll t;
            cin >> t;
            v.push_back(t);
        }

        v.push_back(0);

        for( auto t:v ){
            for( auto s:v ){
                sum_v.push_back(t+s);
            }
        }

        sort(sum_v.begin(),sum_v.end());

        ans=-1;
        for( auto t:sum_v ){
            if( binary_search(sum_v.begin(),sum_v.end(),m-t) ){
                ans=m;
                break;
            }else{
                auto it=upper_bound(sum_v.begin(),sum_v.end(),m-t);
                if( it != sum_v.begin() ){
                    ans=max(ans,t + *(--it));
                }
            }
        }

        cout<<ans<<endl;
    }
}

int main(){
    solve();
    return 0;
}

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

#define MAX_N 1000
int P[MAX_N + 1];
int N, M;
int table[5][MAX_N + 1];

int dfs(int a, int b, int c){
	if(c > M) return P[b] * -1;
	if(a == 0) return P[b];
	if(table[a][b] >= 0) return table[a][b];
	
	int cho = b;
	while(cho <= N){
		int d = dfs(a - 1, cho, c + P[b]) + P[b];
		table[a][b] = max(table[a][b], (d > M)? 0 : d);
		cho++;
	}
	
	return table[a][b];
}

int main(){
	ifstream ifs("input.txt");
	ofstream ofs("output.txt");
	
	for(int i = 0; i < 5; i++){
		for(int j = 0; j <= MAX_N; j++){
			table[i][j] = -1;
		}
	}
	
	ifs >> N >> M;
	
	P[0] = 0;
	for(int i = 1; i <= N; i++) ifs >> P[i];
	
	ofs << dfs(4, 0, 0) << endl;
	
	return 0;
}