Go: ///
// File:  0529.go
// Author: ymiyamoto
//
// Created on Wed Feb 28 20:39:28 2018
//
package main

import (
	"fmt"
	"sort"
)

func max(n, m int) int {
	if n < m {
		return m
	} else {
		return n
	}
}

func main() {
	for {
		var N, M int
		fmt.Scan(&N, &M)
		if N == 0 && M == 0 {
			break
		}

		arr := make([]int, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Ints(arr)

		sums := make([]int, 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.Ints(sums)

		m := 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>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,m,a[1000];
int s[500500],stop;
int main(){
	while(true){
	cin>>n>>m;
	if(!n&&!m)break;
	stop=1,a[0]=0,s[0]=0;
	rep(i,n){
		cin>>a[i+1];
		rep(j,i+2)if(s[i+1]+s[j]<=m)s[stop]=a[i+1]+a[j],stop++;
	}
	sort(s,s+stop);
	int ans=0;
	rep(i,stop){
		int left=0,right=stop,mid,key=m-s[i];
		if(key>=0){
		while(left<right){
			mid=(left+right)/2;
			if(s[mid]<key)left=mid+1;
			else right=mid;
		}
		if(s[mid]>key)mid--;
		if(mid+1)ans=max(ans,s[mid]+s[i]);
		}
	}
	cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1002001];
int main(void){
	while(1){
		ll n,m,i,j,p[1001],hi,lo,mi,pp,x,mx;
		cin>>n>>m;
		if(n==0)break;
		p[0]=0;
		for(i=1;i<=n;i++) cin>>p[i];
		for(i=0;i<=n;i++) for(j=0;j<=n;j++) a[i*n+j]=p[i]+p[j];
		pp=pow(n+1,2);
		sort(a,a+(pp+2));
//		for(i=0;i<=pp+1;i++) cout<<a[i]<<endl;
		mx=0;
		for(i=0;i<=pp+1;i++){
			hi=pp+1; lo=0;
			while(hi>lo){
				mi=(hi+lo)/2;
				x=a[i]+a[mi];
				if(x>m) hi=mi-1;
				else if(x<m) lo=mi+1;
			}
			x=a[i]+a[lo];
			if(mx<x&&m>=x) mx=x;
		}
		cout<<mx<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<queue>
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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int n,m;
vector<int> p;
vector<Int> two;

int main() {
	while(cin>>n>>m,n){
		p.clear();
		two.clear();
		int i;
		for(i=0;i<n;i++){
			p.push_back(in());
		}
		int j;
		for(i=0;i<n;i++){
			for(j=0;j<n;j++){
				two.push_back(p[i]+p[j]);
			}
			two.push_back(p[i]);
		}
		two.push_back(0);
		sort(two.begin(),two.end());
		Int res=0;
		for(i=0,j=two.size()-1;i<two.size()&&j>=0;){
			if(two[i]+two[j]<=m){
				chmax(res,two[i]+two[j]);
				i++;
			}else{
				j--;
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <cmath>
#include <limits>
#include <functional>

#define REP(i,n) for(int i=0;i<n;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 pb(x) push_back(x) 
#define SORT(x) sort((x).begin(),(x).end())
#define INF 999999999

using namespace std;

#define int long long

signed main(){
	int n, m;
	cin >> n >> m;
	vector<int> p(n);
	REP(i, n){
		cin >> p[i];
	}
	p.push_back(0);
	sort(p.begin(), p.end());
	n++;
	
	vector<int> v;
	REP(i, n){
		REP(j, n){
			v.push_back(p[i]+p[j]);
		}
	}
	SORT(v);
	
	int l=0, r=v.size()-1, ans=0;
	for(; l<r; l++){
		if(v[l]+v[r]>m) r--;
		ans=max(ans, v[l]+v[r]);
	}
	
	cout << ans << endl;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int f[210000000];
int main(void)
{
	int a,s,d[1000],high,low,ba,naka,nokori,kore,g,h,i,j,z,x,c,v;
	while(1){
		kore=0;
		scanf("%d %d",&a,&s);
		if(a==0&&s==0)
			break;
		d[0]=0;
		for(i=1;i<=a;i++){
			scanf("%d",&d[i]);
		}
		g=0;
		for(i=0;i<=a;i++){
			for(j=i;j<=a;j++){
				f[g]=d[i]+d[j];
				g++;
			}
		}
		sort(f,f+g);
		/*for(i=0;i<g;i++)
			printf("%d\n",f[i]);*/
		
		for(i=0;i<g;i++){
			v=0;
			low=0;
			high=g-1;
			nokori=s-f[i];
		//	printf("%d %d\n",i,nokori);
			if(nokori>=0){
				while(1){
					if(low>high)
						break;
					naka=(low+high)/2;
				//	printf("%d = %d %d %d %d %d\n",i,f[high],f[naka],f[low],f[i],nokori);
					if(nokori>f[naka]){
						low=naka+1;
					//	printf("%d %d %d low\n",i,nokori,f[naka]);
					}
					if(nokori<f[naka]){
						high=naka-1;
					//	printf("%d %d %d high\n",i,nokori,f[naka]);
					}
					if(nokori==f[naka]){
						ba=f[naka];
						v=1;
						break;
					}
				}
				if(v==0)
					ba=f[low-1];
				//printf("%d\n",f[i]+ba);
				if(kore<f[i]+ba){
					kore=f[i]+ba;
				//	printf("   %d\n",kore);
				}
			}
		}
		printf("%d\n",kore);
	}
	return 0;
}