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++: 
//h08-3 ダーツ
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<set>
#include<stack>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<random>
#include<cassert>
using namespace std;
#include<boost/multiprecision/cpp_int.hpp>
namespace boost_multiprecision = boost::multiprecision;
#define LL long long
#define CINT boost_multiprecision::cpp_int
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -21474836487
#define INT_MAX 2147483647
#define LL_MIN -922337203685477580
#define LL_MAX 9223372036854775807
#define segment_size 65536
#define ROOP() while(true)




int main(){
    ROOP(){
        LL N,M;//Mを超えちゃだめ
        cin >> N >> M;
        if(N==0 && M==0) break;

        vector<LL> P;
        P.push_back(0);
        for(LL i=0; i<N; i++){
            LL tmp;
            cin >> tmp;
            if(tmp <= M) P.push_back(tmp);
        }

        vector<LL> f;
        for(LL i=0; i<P.size(); i++){
            for(LL j=0; j<P.size(); j++){
                LL tmp = P[i] + P[j];
                if(tmp <= M) {
                    f.push_back(tmp);
                    //cout << tmp << endl;
                }
            }
        }
        //cout << "=" << endl;

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

        LL ans = 0;     
        for(LL i=0; i<f.size(); i++){
            LL tmp = M - f[i];
            auto itr = upper_bound(f.begin(),f.end(),tmp);
            itr--;
            ans = max(ans , f[i] + *itr);
        }
        cout << ans << endl;
        //cout << "==" << endl;
        
    }
    
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

#define f first
#define s second

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		
		if(n==0)break;
		
		vector<int> ya;
		ya.push_back(0);
		
		rep(i,n){
			int a;
			cin>>a;
			ya.push_back(a);
		}
		
		vector<int> y2;
		rep(i,ya.size()){
			rep(j,ya.size()){
				y2.push_back(ya[i]+ya[j]);
			}
		}
		
		sort(y2.begin(),y2.end());
		
		
		int maxi=0;
		
		rep(i,y2.size()){
			vector<int>::iterator = (lower_bound(y2.begin(),y2.end(),(m-y2[i]+1))-1);
			maxi= max(maxi,y2[i]+*a);
		}
		printf("%d\n",maxi);
	}
}

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

vector<long long int> p;
vector<long long int> p2;

int main(void){
	int a,b,n,m;
	while(1){
		long long int ans=0;
		scanf("%d%d",&n,&m);
		if(n==0 && m==0)break;
		p.clear();
		p2.clear();
		for(a=0;a<n;a++){
			int num;
			scanf("%d",&num);
			p.push_back(num);
		}
		sort(p.begin(),p.end());
		p2.push_back(0);
		for(a=0;a<n;a++){
			p2.push_back(p[a]);
			for(b=0;b<n;b++){
				p2.push_back(p[a]+p[b]);
			}
		}
		p2.push_back(0);
		sort(p2.begin(),p2.end());
		for(a=0;a<p2.size();a++){
			long long int res=m-p2[a];
			if(m-p2[a]<0)continue;
			long long int pp=*(lower_bound(p2.begin(),p2.end(),res)-1);
			if(ans<pp+p2[a])ans=pp+p2[a];
			//cout << pp+p2[a] << endl;
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>


using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
int dx[4] = { 1,0,0,-1 };
int dy[4] = { 0,1,-1,0 };

#define ARRAY_MAX 100005

int main() {


	ll n, m;
	while (cin >> n >> m, n | m)
	{
	
		vector<ll> a(n + 1, 0);

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

		vector<ll> vec;

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

			}
		}

		sort(vec.begin(), vec.end());
		ll siz = vec.size();
		ll maxi = 0;
		for (ll i = 0; i < siz; i++)
		{
			auto itr = upper_bound(vec.begin(), vec.end(), m - vec[i]);

			if (itr == vec.begin())
			{
				if (vec[i] <= m)
				{
					maxi = max(maxi, vec[i]);
				}
			}
			else
			{
				itr--;
				if (vec[i] + *itr <= m)
				{
					maxi = max(maxi, vec[i] + *itr);
				}
			}
			
		}

		a.shrink_to_fit();
		vec.shrink_to_fit();
		cout << maxi << endl;
	}
	

	return 0;
}

C++: #include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
    long long n, m;
    while (cin >> n >> m, n) {
        vector <long long> p(n);
        rep (i, n) cin >> p[i];
        p.push_back(0);

        vector <long long> sum[2];
        rep (i, 2) rep (j, n + 1) rep (k, n + 1) sum[i].push_back(p[j] + p[k]);

        long long ans = 0;
        sort(sum[1].begin(), sum[1].end());
        for (long long num : sum[0]) {
            auto tmp = lower_bound(sum[1].begin(), sum[1].end(),
                                   m - num
                                   );
            if (tmp != sum[1].begin()) tmp--;
            ans = max(ans,
                      num + *tmp > m ? 0 : num + *tmp
                      );
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
int main() {
	ll a[1000], n, m;
	while (cin >> n >> m&&n != 0 && m != 0) {
		for (int i = 0; i < n; i++) cin >> a[i];
		sort(a, a + n);
		int t = 0;
		for (t; t < n; t++) {
			if (a[t] > m) break;
		}
		t--;
		long long sum = 0;
		for (int i = t; i >= 0; i--) {
			sum = max(sum, a[i]);
			for (int j = i; j >= 0; j--) {
				if (a[i] + a[j] <= m) {
					sum = max(sum, a[i] + a[j]);
					for (int x = j; x >= 0; x--) {
						if (a[i] + a[j] + a[x] <= m) {
							sum = max(a[i] + a[j] + a[x], sum);
							for (int y = x; y >= 0; y--) {
								if (a[i] + a[j] + a[x] + a[y] <= m) {
									sum = max(a[i] + a[j] + a[x] + a[y], sum);
									goto stop;
								}
							}
						}
					}
				}
			}
		}
	stop:;
		cout << sum << endl;
	}
}