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

using namespace std;

int n , m;
vector<int> point, points;

int main()
{
	int c;
	while(cin >> n >> m){
		if(n == 0, m == 0) break;
		point.push_back(0);

		for(int i = 0; i < n; i++)
		{
			cin >> c;
			point.push_back(c);
		}

		n++;

		for(int i = 0; i < n; i++)
		{
			for(int j = i; j < n; j++)
			{
				points.push_back(point[i]+point[j]);
			}
		}
		int ans = 0, left, right, mid;

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

		for(int i = 0; i < points.size(); i++)
		{
			left = 0;
			right = points.size();

			int temp = m - points[i];

			if(temp < 0) continue;

			while(right - left > 1)
			{
				mid = (right + left) / 2;
				if(temp < points[mid]) right = mid;
				else if(temp > points[mid]) left = mid;
				else if(temp == points[mid]) break;
			}
			ans = max(ans, points[i] + points[left]);
		}
		cout << ans << endl;
		point.clear();
		points.clear();
	}
}

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

set<long long> sep;
long long ary[1010];
int main(){
	int N;
	long long M,res;
	set<long long>::iterator it,it2;
	while(1){
		sep.clear();
		cin>>N>>M;
		if(N==0&&M==0) break;
		for(int i=0;i<N;i++){
			cin>>ary[i];
		}
		ary[N]=0;
		for(int i=0;i<=N;i++){
			for(int j=i;j<=N;j++){
				if(ary[i]+ary[j]<=M){
					sep.insert(ary[i]+ary[j]);
				}
			}
		}
		res=0;
		it2=sep.end();
		it2--;
		for(it=sep.begin();it!=sep.end();it++){
			while((*it)+(*it2)>M)it2--;
			if((*it)>(*it2)) break;
			res=max(res,(*it)+(*it2));
		}
		cout<<res<<endl;

	}
	return 0;
}

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

int main(){
	cin.tie(0),ios::sync_with_stdio(false);
	while(true){
		int n,m; cin>>n>>m;
		if(n==0&&m==0)break;
		vector<int>p(n); for(auto&&i:p)cin>>i; p.emplace_back(0);
		vector<int>b;
		for(int i=0;i<=n;++i){
			for(int j=i;j<=n;++j){
				b.emplace_back(1L*p.at(i)+p.at(j));
			}
		}
		sort(b.begin(),b.end());
		int ans=0;
		for(int i=0;i<n;++i){
			for(int j=i;j<n;++j){
				int L=0,R=b.size(),score=p.at(i)+p.at(j),mx=m-score;
				if(mx<0)continue;
				while(R-L>1){
					int mid=(L+R)/2;
					if(b.at(mid)>mx)R=mid;
					else L=mid;
				}
				score+=b.at(L);
				if(score<=m)ans=max(ans,score);
			}
		}
		cout<<ans<<"\n"s;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 1002

long long p[MAX], dp[MAX];
int main(){
	long long n, m, ans;
	while(cin >> n >> m, n){
		fill(dp, dp+MAX, 0);
		for(int i = 0;i < n;i++)cin >> p[i];
		ans = 0;
		
		for(int i = 0;i < 3;i++){
			dp[0] = 5;
			for(int j = p[i];j <= m;j++){
				dp[j] = max(dp[j], dp[j-p[i]]-1);
				if(dp[j])ans = max(ans, j);
			}
		}
		//for(int i = 1;i <= 50;i++)cout << dp[i] << endl;
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main(){
  int n,m;
  while(n = getInt(), m = getInt(), n+m){
    set<int> s; s.insert(0);
    REP(i,n) s.insert(getInt());
    REP(k,2){
      set<int> s2;
      FOR(it,s){
        for(set<int>::iterator it2 = it; it2 != s.end(); ++it2){
          int sum = *it + *it2;
          if(sum <= m)
            s2.insert(sum);
        }
      }
      s = s2;
    }

    if(s.size() == 1) print(0);
    else print(*(--(s.end())));
  }
  return 0;
}