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 <iostream>
#include <algorithm>
#include <functional>
#include <cstring>
#include <cstdlib>

using namespace std;

int array_sum( int ary[4][1001], int idx, int size )
{
	int sum = 0;
	for(int i=0; i < size; ++i) sum += ary[i][idx];
	return sum;
}
void map_sort( int ary[4][1001], int idx, int size )
{
	for(int i=0; i < size; ++i)
		for(int j=i+1; j < size; ++j) {
			if(ary[i][idx] < ary[j][idx])
				swap(ary[i][idx], ary[j][idx]);
		}
}

int main( void )
{
	int n=0, m, input[1002], map[4][1001];
	while(cin >> n >> m, n | m) {
		for(int i=0; i < n; ++i) {
			cin >> input[i];
			for(int j=0; j < 4; ++j) map[j][i] = 0;
		}
		for(int i=0; i < 4; ++i) map[i][n] = 0;
		sort(input, input+n, greater<int>());
		int sum = 0, count = 0;
		while(sum < m && count < 4) {
			sum += input[0];
			++count;
		}
		int mmm = m, cc = 0;
		for(int i=0; i < n; ++i) {
			while(input[i] <= mmm && cc < 4) {
				mmm -= input[i];
				++cc;
			}
			if(cc >= 4) break;
		}
		sort(input+1, input+n);
		for(int i=0; i < count; ++i) map[i][0] = input[0];
		int sub = sum - m;
		if(!sub) { cout << m << endl; continue; }
		else if(sub < 0) { cout << sum << endl; continue; }
		int Min = min(m, mmm);
		//cout << Min << endl;
		input[n] = 0;
		for(int i=0; i < n; ++i) {
			//cout << input[i] << endl;
			sum = array_sum(map, i, count);
			if(sum == m) { Min = 0; break; }
			int next = i + 1, ii = i + 1;
			for(int j=0; j < count; ++j) {
				int tsum = sum - map[j][i] + input[ii];
				int tsub = tsum - m;
				if(tsub > 0) {
					if(sum > tsum) {
						sum = tsum;
						map[j][next] = input[ii];
					}
					else
						map[j][next] = map[j][i];
				}
				else if(!tsub) {
					Min = 0; break;
				}
				else { // minus
					Min = min(Min, -tsub);
					int tcount = 4 - count;
					int tmul = tcount * input[ii];
					if(tmul >= -tsub) {
						for(int k=0; k <= j; ++k) map[k][next] = input[ii];
						for(int k=j+1; k < count; ++k) map[k][next] = map[k][i];
						for(int k=count; k < 4; ++k) {
							if(sum < array_sum(map, next, k) + input[ii]) break;
							map[k][next] = input[ii];
							++count;
						}
						//input[ii] = 0;
						//i -= 2;
						break;
					}
					else {
						map[j][next] = map[j][i];
					}
				}
			}
			if(!Min) break;
			map_sort(map, next, 4);
			//cout << "--------------------------------------------" << endl;
			//for(int y=0; y < 4; ++y, cout << endl)
			//	for(int x=0; x < n+1; ++x) cout << map[y][x] << ' ';
			//cout << "--------------------------------------------" << endl;
		}
		if(!Min) { cout << m << endl; continue; }
		for(int i=0; i < count; ++i) map[i][0] = input[0];
		for(int i=0; i < n; ++i) {
			sum = array_sum(map, i, count);
			if(sum == m) { Min = 0; break; }
			int next = i + 1, ii = i + 1;
			for(int j=0; j < count; ++j) {
				int tsum = sum - map[j][i] + input[ii];
				int tsub = tsum - m;
				if(tsub > 0) {
					if(sum > tsum) {
						sum = tsum;
						map[j][next] = input[ii];
					}
					else
						map[j][next] = map[j][i];
				}
				else if(!tsub) {
					Min = 0; break;
				}
				else { // minus
					Min = min(Min, -tsub);
					map[j][next] = map[j][i];
				}
			}
			if(!Min) break;
			map_sort(map, next, 4);
		}
		cout << m - Min << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
int main(){
while(1){
int i,n,j,m,s=0,l,k;
static int p[1001]={0};
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++)scanf("%d",&p[i]);
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
for(l=0;l<=n;l++){
for(k=0;k<=n;k++){
if(p[i]+p[j]+p[l]+p[k]<=m&&p[i]+p[k]+p[j]+p[l]>s)s=p[i]+p[j]+p[l]+p[k];
}
}
}
}
printf("%d\n",s);
}
return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define mp(x,y) make_pair(x,y)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int,int>
#define INF 1<<29
#define MAX 1010
using namespace std;

int p[MAX];

void solve(){
 int n,m,ind;
 for(;;){
  cin>>n>>m;
  if(!n) break;
  int ma=0;
  vector<int> v;
  FOR(i,n){
   cin>>p[i];
   v.pb(p[i]);
  }
  FOR(i,n){
   FORR(j,i,n){
    v.pb(a[i]+a[j]);
   }
  }
  sort(v.begin(),v.end());
  erase(unique(v.begin(),v.end()),v.end());
  ind=upper_bound(v.begin(),v.end(),m)-1;
  if(ind>=0) ma=v[ind];
  FOR(i,v.size()){
   ind=upper_bound(v.begin(),v.end(),m-v[i])-1;
   if(ind>=0) ma=max(ma,v[i]+v[ind]);
  }
  cout<<ma<<endl;
 }
}

int main(){
 solve();
 return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#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};

llong solve(int n, llong m){
	llong p[n];
	REP(i, n) cin >> p[i];
	sort(p, p+n);
	
	llong res=0;
	int c=0;
	REPR(i, n-1){
		while(res<=m && c<4){
			res+=p[i];
			c++;
		}
		if(res>m){
			res-=p[i];
			c--;
		}
	}
	
	return res;
}

int main(){
	while(1){
		int n;
		llong m;
		cin >> n >> m;
		if(n==0 && m==0) return 0;
		cout << solve(n, m) << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_V 1000000

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	vector<int> rsl;
	while(true) {
		ll N, M;
		cin >> N >> M;
		if(N == 0 and M == 0) break;

		vector<ll> P;
		rep(i, 0, N) {
			ll p;
			cin >> p;
			P.push_back(p);
		}

		vector<ll> kk;

		rep(i, 0, N + 1) rep(j, 0, N + 1) kk.push_back(-P[i] + -P[j]);

		vsort(kk);

		vector<ll> tmp;
		rep(i, 0, N + 1) rep(j, 0, N + 1) {
			ll tmp_point = P[i] + P[j] + -*lower_bound(kk.begin(), kk.end(), -(M - P[i] - P[j]));
			if(tmp_point > M) continue;
			tmp.push_back(tmp_point);
		}
		vsort(tmp);
		rsl.push_back(tmp[tmp.size() - 1]);
	}
	rep(i, 0, rsl.size()) cout << rsl[i] << endl;


}