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 main() {
	int n, m;
	while (cin >> n >> m, n + m) {
		vector<int> p(n + 1), tmp;
		for (int i = 0; i < n; ++i) cin >> p[i];
		p[n] = 0;
		for (int i = 0; i <= n; ++i)
			for (int j = i; j <= n; ++j)
				if (p[i] + p[j] <= m) tmp.push_back(p[i] + p[j]);
		sort(tmp.begin(), tmp.end());
		int ans = 0;
		for (int i = 0; i < tmp.size(); ++i)
			ans = max(ans, tmp[i] + *(upper_bound(tmp.begin(), tmp.end(), m - tmp[i]) - 1));
		cout << ans << endl;
	}
		
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
typedef long long L;
using namespace  std;
int main(){
	int n;
	L m,p[1001],sum=0;
	vector<L> sum1;
	vector<L>::iterator it,ele;
	scanf("%d %lld",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%lld",&p[i]);
	}
	p[n]=0;
	for(int i=0;i<=n;i++){
		for(int i2=0;i2<=n;i2++){
			sum1.push_back(p[i]+p[i2]);
		}
	}
	sort(sum1.begin(),sum1.end());
	for(it=sum1.begin();it<sum1.end();it++){
		if(*it>m)
			break;
		ele=upper_bound(sum1.begin(),sum1.end(),(m-(*it) ) );
		if(ele!=sum1.begin()){
			ele--;
		}
		if(sum<=(*it)+(*ele)){
			sum=(*it)+(*ele);
		}
		if(sum==m)
			break;
	}
	printf("%lld\n",sum);
	return 0;
}

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

vector<long long> sep;

long long ary[1010];
int main(){
	int N;
	long long M,res;
	vector<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.push_back(ary[i]+ary[j]);
				}
			}
		}
		sort(sep.begin(),sep.end());
		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 <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long n;
	unsigned long long m;
	while(cin>>n>>m,n!=0||m!=0){
		int k=0;
	unsigned long long  s=0;
		vector<unsigned long long> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>=0&&k<4){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
				k++;
			}
			else
				n--;
			
		}
		cout<<s<<endl;
	}
	
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> a;
vector <int> all;
int n,m;


int main() {

  while(1) {
    scanf("%d%d",&n,&m);
    if(n == 0 && m ==0) break;
    a.resize(n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);

    a.push_back(0);
    all.clear();

    for(int i=0;i<a.size();i++) 
      for(int j=i;j<a.size();j++)
	all.push_back(a[i]+a[j]);

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

    int ans = 0;
    int allsize = all.size();
    int b = 0;
    for(int i=allsize-1;i>= 0;i--) {
      for(int j=b;j<allsize;j++) {
	int sum = all[i] + all[j];
	if(sum > m) break;
	b++;
	if(ans < sum) ans = sum;
      }
      if(ans == m) break;
    }

    cout << ans <<endl;
  }
  return 0;
  
}

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

int n, m;
vector<int> p;
int t;
vector<int> q;
vector<int> o;
int ans;

int b(int x){
	int l = 0;
	int r = q.size();
	while(r - l > 1){
		int i = (l + r) / 2;
//		printf("l=%d, r=%d, i=%d, q[i]=%d, x=%d\n", l, r, i, q[i], x);
		if(q[i] <= x){
			l = i;
		}else{
			r = i;
		}
	}
//	printf("%d\n", q[l]);
	return q[l];
}

int main(){
	while(1){
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;
		p = o;
		p.push_back(0);
		for(int i=0; i<n; i++){
			scanf("%d", &t);
			p.push_back(t);
		}
		q = o;

		for(int i=0; i<p.size(); i++){
			for(int j=0; j<p.size(); j++){
				q.push_back(p[i] + p[j]);
			}
		}
		sort(q.begin(), q.end());
/*
		for(int i=0; i<q.size(); i++)
			printf("%d\n", q[i]);
*/
		for(int i=0; i<q.size(); i++){
			int a = q[i];
			if(m - a < 0) continue;
			ans = max(ans, b(m - a) + a);
//			printf("%d\n", b(m - a));
		}

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

	return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
//http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
#include<cstring>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define P pair<int,int>
#define ll long long 

ll n,m;
ll t[10000];
const int MA=2000000;
ll s1[MA];
ll s2[MA];


int main(){

while(cin>>n>>m,n||m){
rep(i,MA)s1[i]=s2[i]=0;
rep(i,10000)t[i]=0;

for(int i=1;i<=n;i++)cin>>t[i];
int g=0;

for(int i=0;i<=n;i++){
	for(int j=0;j<=n;j++){
		s1[i*(n+1)+j]=t[i]+t[j];
		g++;
	}
}


sort(s1,s1+g);
rep(i,g)s2[i]=s1[i];

ll ans=0;

for(int i=0;i<g;i++){
	int now=s1[i];

	int l=0;
	int r=g-1;
	int mid=(l+r)/2;
	ll x=s2[mid];

	for(int i=0;i<70;i++){
		if(x+now==m)break;
		if(l>r)break;
		if(x+now>m){
			r=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
		else{
			l=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
	}

	//cout<<" "<<x<<" "<<now<<" "<<x+now<<endl;
	if(x+now<=m)
	ans=max(ans,x+now);
}
cout<<ans<<endl;
}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#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;
		else if(v[mid]<t){
			if(v[mid+1]>t) return v[mid];
			else l=mid+1;
		}
		else break;
	}
	
	return v[r-1];
}

llong solve(vector<llong> p){
	vector<llong> v;
	set<llong> st;
	int c=0;
	REP(i, n){
		REPR(j, n-1){
			if(st.find(p[i]+p[j])==st.end()){
				v.push_back(p[i]+p[j]);
				st.insert(v[c]);
				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;	
	}
}