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++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(int i=0;i<N;++i)
typedef long long int LL;

const LL inf = 112345678901;
LL N, M, P[1123];
LL memo[1123][5];

LL dp(LL p, LL c)
{
	LL res = 0;
	if (p == -1) { return 0; }
	if (c > 4) { return memo[p][c] = 0; }
	if (memo[p][c] != -1) { return memo[p][c]; }
	if (dp(p - 1, c + 1) + P[p] <= M) { res = dp(p - 1, c + 1) + P[p]; }
	if (dp(p, c + 1) + P[p] <= M) { res = std::max(res, dp(p, c + 1) + P[p]); }
	if (dp(p - 1, c) <= M) { res = std::max(res, dp(p - 1, c)); }
	return memo[p][c] = res;
}

int main()
{
	while (true)
	{
		N = M = 0;
		memset(P, 0, sizeof(P));
		memset(memo, -1, sizeof(memo));

		in >> N >> M;
		if (N == 0 && M == 0) { break; }
		rep(i, N) { in >> P[i]; }
		std::sort(P, P + N);
		out << dp(N - 1, 0) << std::endl;
	}
	return 0;
}

C++: #include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int N, M, D;

	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

		if (N == 0 && M == 0) { break; }

		vector<int> C; C.push_back(0);

		for (int i = 0; i < N; i++)
		{
			scanf("%d", &D);

			if (D <= M)
			{
				C.push_back(D);
			}
		}

		vector<int> r;

		for (int i = 0; i < C.size(); i++)
		{
			for (int j = i; j < C.size(); j++)
			{
				if (C[i] + C[j] <= M)
				{
					r.push_back(C[i] + C[j]);
				}
			}
		}

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

		int ret = 0, lc = 0, rc = r.size() - 1;

		while (rc - lc >= 0)
		{
			if (r[lc] + r[rc] > M)
			{
				rc--;
			}
			else
			{
				ret = max(ret, r[lc] + r[rc]); lc++;
			}
		}

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

	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, p[1010];
vector<int> vi;

int main(){
	while (cin >> n >> m&&n + m){
		vi.clear();
		p[0] = 0;
		FOR(i, 1, n + 1)cin >> p[i];
		REP(i, n + 1){
			REP(j, n + 1){
				if (p[i] + p[j] <= m){
					vi.push_back(p[i] + p[j]);
				}
			}
		}
		int ans = -1;
		sort(ALL(vi));
		vi.erase(unique(ALL(vi)), vi.end());
		REP(i, vi.size()){
			REP(j, vi.size()){
				if (vi[i] + vi[j] <= m){
					ans = max(ans, vi[i] + vi[j]);
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
	int n, m, a[1001], t, p, q, r;
	bool f;
	
	while(cin>>n>>m&&n&&m){
		vector<int> vec;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
		}
		a[0] = 0;
		
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				r = a[i]+a[j];
				f = true;
				for(int k = 0; k < vec.size(); k++){
					if(r == vec[i]){
						f = false;
						break;
					}
				}
				if(f){
					vec.push_back(r);
				}
			}
		}
		sort(vec.begin(), vec.end());
		
		t = 0;
		for(int i = 0; i < vec.size(); i++){
			p = 0;
			q = vec.size();
			while(q-p > 1){
				r = (p+q)/2;
				if(vec[i]+vec[r] >= m){
					q = r;
				} else {
					p = r;
				}
			}
			r = vec[i]+vec[p];
			if(t < r && r <= m){
				t = r;
			}
		}
		
		cout << t << endl;
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
vector<long int> po;
vector <pair <int ,long int> > sss;
long int n,a,low,m,lastans;
map <int, int> mp;
void solve(int dp,long int sum);

int main(){
    while(1){
        lastans=0;
        po.clear();
        mp.clear();
        //for(int i=0;i<1005;i++)memo[i]=0;

        cin >> n >> m;
        if(n==0&&m==0)break;
        for(int i=0;i<n;i++){
            cin >> a;
            po.push_back(a);
           // low=min(low,a);
        }
        sort(po.begin(),po.end());
        //cout << po[0] << endl;
        //solve(0,0);
        //lastans=po[n-1];

        for(int i=4;i>0;i--){
            for(int j=n-1;j>=0;j--){
                if(mp[i*po[j]]>0);
                else{
                if(m>=i*po[j]){
                    lastans=max(i*po[j],lastans);
                    mp[i*po[j]]++;

                    for(int k=4-i;i>0;i--){
                        for(int s=0;s<n;s++){
                            if(m>=i*po[j]+po[s]){
                                lastans=max(i*po[j]+po[s],lastans);
                                mp[po[s]]++;

                                for(int t=4-k;t>0;t--){
                                    for(int u=0;u<n;u++){
                                        if(m>=i*po[j]+po[s]+po[u]){
                                         lastans=max(i*po[j]+po[s]+po[u],lastans);
                                         mp[po[u]]++;

                                         for(int v=4-t;v>0;v--){
                                            for(int w=u-1;w>=0;w--){
                                                if(m>=i*po[j]+po[s]+po[u]+po[w]){
                                                    lastans=max(lastans,i*po[j]+po[s]+po[u]+po[w]);
                                                    mp[po[w]]++;
                                                    //break;
                                                }else break;
                                            }
                                         }
                                            //break;
                                        }else break;
                                    }
                                }
                                //break;
                            }else break;

                        }
                    }

                    //lastans=max(lastans,po[j]*i);
                    //break;
                }else break;
            }
            }
        }



        cout << lastans << endl;
        //cout <<"ss=" << ss << endl;
    }

    return 0;
}