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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, p[1010];
int dp[1010];

int main(){
	while (cin >> n >> m&&n + m){
		int i = 0;
		/*fill(dp, dp + 1010, 0);
		p[0] = 0;
		FOR(i, 1, n + 1)cin >> p[n];
		REP(i, 4){
			REP(j, n + 1){
				int now = dp[j];
				REP(k, n + 1){
					if (dp[j] + p[k] <= m){
						
					}
				}
			}
		}*/
		REP(i, m){
			i++;
		}
		cout << i << 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);
		}
		P.push_back(0);

		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) tmp.push_back(0);
			else tmp.push_back(tmp_point);
		}
		vsort(tmp);
		rsl.push_back(tmp[tmp.size() - 1]);
	}
	rep(i, 0, rsl.size()) cout << rsl[i] << endl;


}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int table[1100000];
int val[1001];
int main(){
	int a,b;
	while(1){
		scanf("%d%d",&a,&b);
		if(a+b==0)return 0;
		for(int i=0;i<a;i++){
			scanf("%d",val+i);
		}
		val[a]=0;
		for(int i=0;i<=a;i++){
			for(int j=0;j<=a;j++){
				table[i*(a+1)+j]=val[i]+val[j];
			}
		}
		std::sort(table,table+(a+1)*(a+1));
		int point=0;
		for(int i=0;i<=a;i++){
			for(int j=0;j<=a;j++){
				if(val[i]+val[j]<=b)point=max(point,val[i]+val[j]+table[upper_bound(table,table+(a+1)*(a+1),b-val[i]-val[j])-table-1]);
			}
		}
		printf("%d\n",point);
	}
}

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

int main(){
while(1){
	int n,m;
	vector<int> p,q;
	scanf("%d%d",&n,&m);
if(n==0&&m==0)break;
	q.push_back(0);
	for(int i=0;i<n;i++){
		int a;
		scanf("%d",&a);
		p.push_back(a);
		q.push_back(a);
	}
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			q.push_back(p[i]+p[j]);
		}
	}
	sort(q.begin(),q.end());
	int ans=0;
	int f=0,s=q.size()-1;
	while(1){
		if(q[f]+q[s]>ans&&q[f]+q[s]<=m)ans=q[f]+q[s];
		if(q[f]+q[s]<=m)f++;
		if(q[f]+q[s]>m)s--;
		if(f>s)break;
	}
	printf("%d\n",ans);
}
}

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

using namespace std;

int main()
{
	int n, m, a[1001], t, p, q, r;
	bool f;
	
	while(cin>>n>>m&&n&&m){
		vector<int> vec;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
		}
		a[0] = 0;
		
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				r = a[i]+a[j];
				f = true;
				for(int k = 0; k < vec.size(); k++){
					if(r == vec[i]){
						f = false;
						break;
					}
				}
				if(f){
					vec.push_back(r);
				}
			}
		}
		sort(vec.begin(), vec.end());
		
		t = 0;
		for(int i = 0; i < vec.size(); i++){
			p = 0;
			q = vec.size();
			while(q-p > 1){
				r = (p+q)/2;
				if(vec[i]+vec[r] >= m){
					q = r;
				} else {
					p = r;
				}
			}
			r = vec[i]+vec[p];
			if(t < r && r <= m){
				t = r;
			}
		}
		
		cout << t << endl;
	}
	
	return 0;
}

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

int main(){
	int n,m,t;
	while(cin >> n >> m, n){
		vector<int> data;
		for(int i=0;i<n;i++){
			cin >> t;
			data.push_back(t);
		}
		data.push_back(0);

		
		vector<int> ret;
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++){
				ret.push_back(data[i]+data[j]);
			}
		}
		
		sort(ret.begin(),ret.end());
		
		int r = 0;
		for(int i=0;i<ret.size();i++){
			int t = m - ret[i];
			if( t < 0 )continue;
			r = max(r,ret[i] + *(lower_bound(ret.begin(),ret.end(),t)-1));
		}
		cout << r << endl;
	}
}

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define mp(a,b) make_pair((a),(b))
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int INF=1<<30;
const long long int INF_=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

void Solve(){
	ll n,m;
	while(true){
		cin >> n >> m;
		if(n==0&&m==0) break;
		
		vector<ll> p;
		p.push_back(0);
		rep(i,0,n){
			ll in;
			cin >> in;
			p.push_back(in);
		}

		vector<ll> a;
		rep(i,0,p.size()){
			rep(j,0,p.size()){
				a.push_back(p[i]+p[j]);
			}
		}
		sort(a.begin(),a.end());

		ll ans=0;
		rep(i,0,p.size()){
			rep(j,0,p.size()){
				ll s=p[i]+p[j];
				int idx=upper_bound(a.begin(),a.end(),m-s)-a.begin()-1;
				if(idx==-1) continue;
				ans=max(ans,s+a[idx]);
			}
		}

		cout << ans << endl;
	}
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	Solve();
	return 0;
}

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

int dp[100000000];

int main(){
	memset(dp,-1,sizeof(dp));
	/*int N,M;
	while(cin>>N>>M,N||M){
		int dp[200000001]={},P,ans=0;
		dp[0]=1;
		for(int i=0;i<N;i++){
			cin>>P;
			for(int j=M;j>=0;j--){
				
			}
		}
	}*/
	return 0;
}