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 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 <cstdio>
#include <set>
#include <algorithm>
using namespace std;

int N,M,P[1001];
set<int> s;

int main(){
	while(scanf("%d%d",&N,&M), N != 0){
		s = set<int>();
		s.insert(0);
		for(int i = 0; i < N; i++){
			scanf("%d",&P[i]);
			s.insert(P[i]);
		}
		
		for(int i = 0; i < N; i++){
			for(int j = i; j < N; j++){
				s.insert(P[i] + P[j]);
			}
		}

		int mx = 0;
		
		for(set<int>::iterator itr = s.begin(); itr != s.end(); itr++){
			set<int>::iterator tmp = lower_bound(s.begin(),s.end(),M-*itr);
			if(*itr + *tmp > M){
				if(tmp != s.begin()){
					tmp--;
				}
				else{
					continue;
				}
			}
			if(mx < *itr + *tmp){
				mx = *itr + *tmp;
			}
		}

		printf("%d\n",mx);
	}
	return 0;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define MP make_pair

typedef vector<int> vi;

int main() {
	while (true) {
		int n, m, score[1001];
		vi v;
		cin >> n >> m;
		if (n == 0 && m == 0) break;
		REP(i, n) scanf("%d", &score[i]);
		score[n++] = 0;

		REP(i, n) FOR(j, i, n) v.push_back(score[i] + score[j]);
		sort(v.begin(), v.end(), greater<int>());

		int ans = 0;
		REP(i, n) FOR(j, i, n) {
			vi::iterator it = upper_bound(v.begin(), v.end(), m - score[i] - score[j], greater<int>());
			if (it == v.end()) continue;
			ans = max(ans, *it + score[i] + score[j]);
		}

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

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector<long long int> data(n);
		for (int i = 0; i < n; i++) {
			cin >> data[i];
		}
		sort(data.begin(), data.end());
		long long int MAX = 0;
		int sz = data.size();
		for (int j = 0; j < sz; j++) {
			long long int a = m;
			if (data.size() == 0) {
				break;
			}
			for (int i = 0; i < 4; i++) {
				vector<long long int>::iterator ite = upper_bound(data.begin(), data.end(), a);
				if (ite != data.begin()) {
					a -= *(ite-1);
				}
			}
			MAX = max(MAX, m-a);
			vector<long long int>::iterator ite2 = data.end();
			data.erase(ite2-1);
		}
		cout << MAX << endl;
	}
	return 0;
}

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

int main(){
	int N,M;
	while(cin>>N>>M,N||M){
		map<int,int> dp;
		int S,P,latte,ans=0;
		dp[0]=0;
		for(int i=0;i<N;i++){
			cin>>P;
			for(map<int,int>::iterator itr=dp.end();itr!=dp.begin();){
				itr--;
				for(int j=1;;j++){
					latte = itr->first+P*j;
					if(latte>M || itr->second+j>4) break;
					map<int,int>::iterator is_find = dp.find(latte);
					if(is_find==dp.end()){
						//printf("%d,%d ",latte,itr->second+j);
						ans=max(ans,latte);
						dp[latte]=itr->second+j;
					}
					else{
						if(is_find->second > itr->second+j){
							is_find->second = itr->second+j;
						}
					}
				}
			}//puts("");
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>

using namespace std;

#define sz			size()
#define lgt			length()
#define rep(i,n)	for (int i = 0; i < n; i++)
#define ALL(X)		(X).begin(),(X).end()
#define RALL(X)		(X).rbegin(),(X).rend()
#define pb			push_back
#define SORT(a)		sort(ALL(a));
#define RSORT(a)	sort(RALL(a));
#define mp			make_pair
#define dump(x)		cerr<<#x<<" = "<<(x)<<endl;
#define fir			first
#define sec			second

typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<string> vs;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
vs form(string a,char s){vs v;string b;rep(i,a.lgt){if(a[i]==s){v.pb(b);b="";}else b+=a[i];}v.pb(b);return v;}
vi formint(string a,char s){vi v;string b;rep(i,a.lgt){if(a[i]==s){v.pb(toInt(b));b="";}else b+=a[i];}v.pb(toInt(b));return v;}

const double EPS=1e-10,PI=acos(-1.0);
const int INF=2100000000;

int main(void)
{
	int n, m, tmp;
	while (cin >> n >> m, n && m){
		int res = 0;
		vi v, v1;
		
		v.pb(0);
		for (int i = 0; i < n; i++){
			cin >> tmp;
			v.pb(tmp);
		}
		for (int i = 0; i < v.sz; i++){
			for (int j = 0; j < v.sz; j++){
				v1.pb(v[i] + v[j]);
				v2.pb(v[i] + v[j]);
			}
		}
		SORT(v1);
		
		for (int i = 0, j = v1.sz - 1; i < v1.sz && 0 <= j;){
			if (m < v1[i] + v1[j]) j--;
			else {
				res = max(res, v1[i] + v1[j]);
				i++;
			}
		}
		
		cout << res << endl;
		v.clear();
	}
	
	return 0;
}