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 <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;
int main(){
    while(true){
	ll n,m;
	cin >> n >> m;
	if(n == 0 && m == 0) break;
	n++;
	vector<ll> dp[2];
	vector<ll> a(n);
	a[0] = 0;
	FOR(i,1,n){
	    cin >> a[i];
	}
	ll ans = 0;
	FOR(i,0,n)FOR(j,i,n)FOR(k,j,n)FOR(l,k,n){
	    if(a[i]+a[j]+a[k]+a[l] <= m) ans = max(ans,a[i]+a[j]+a[k]+a[l]);
	}
	cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

void solve(int N, int M, int P[1010])
{
	int score = 0;
	sort(P, P + N, greater<int>());
	
	for(int j = 0; j < 4; j++){		
		int i = 0;
		while(true){
			if(score + P[i] > M){
				i++;
			}
			else{
				score += P[i];
				break;
			}
		}
	}
	
	cout << score << endl;
}


int main()
{
		int N, M;
		int P[1010];

	while(true){
		cin >> N;
		cin >> M;
		if(N == 0 && M == 0) break;

		for(int i = 0; i < N; i++){
			cin >> P[i];
		}

		solve(N, M, P);
	}

	return 0;
}

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

int n;
long m, p[1001];
long dp[6][1001];

long rec(int a, int b){ //a本目，最後に当てた位置b
	if(dp[a][b]>=0) return dp[a][b];
	
	long ret=0;
	
	if(a==4) ret=0;
	else{
		for(int i=b; i<=n; ++i){
			long tmp=rec(a+1, i)+p[i];
			if(tmp>m) tmp=0;	
			
			if(ret<tmp) ret=tmp;
		}		
	}
	
	return dp[a][b]=ret;
}

int main(){
	while(1){
		scanf(" %d %ld", &n, &m);
		if(n==0) break;
		p[0]=0; //投げない選択肢
		for(int i=1; i<=n; ++i) scanf(" %ld", &p[i]);
		sort(p, p+n);
		
		memset(dp, -1, sizeof(dp));
		printf("%ld\n", rec(0,0));
	}
}

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

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	ll N, M;
	ll P[1010];
	ll res = 0;
	vector<int> two;
	cin >> N >> M;
	REP(i, N){
		cin >> P[i];
	}
	P[N] = 0;
	REP(i, N+1){
		REP(j, N+1){
			if(P[i]+P[j] <= M)
				two.push_back(P[i]+P[j]);
		}
	}
	sort(all(two));
	REP(i, N+1){
		REP(j, N+1){
			ll a = P[i]+P[j];
			if(a > M) continue;
			res = max(res, a+(*(upper_bound(all(two), M-a)-1)));
		}
	}
	cout << res << endl;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

#define f first
#define s second

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		
		if(n==0)break;
		
		vector<int> ya;
		ya.push_back(0);
		
		rep(i,n){
			int a;
			cin>>a;
			ya.push_back(a);
		}
		
		vector<int> y2;
		rep(i,ya.size()){
			rep(j,ya.size()){
				y2.push_back(ya[i]+ya[j]);
			}
		}
		
		sort(y2.begin(),y2.end());
		
		
		int maxi=0;
		
		rep(i,y2.size()){
			vector<int>::iterator a = lower_bound(y2.begin(),y2.end(),(m-y2[i]+1));
			if(a==y2.begin())continue;
			//printf("%d %d\n",y2[i],*(a-1));
			maxi= max(maxi,y2[i]+*(a-1));
		}
		printf("%d\n",maxi);
	}
}

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

#define MAX_N 1001

using namespace std;

class CNum{
public:
	vector<int> v;
};

bool Find(vector<int> &v, int search){
	for(unsigned int i = 0; i < v.size(); i++){
		if( v[i] == search ){
			return true;
		}
	}
	return false;
}

int main(void){
	while( 1 ){
		int n, m;
		int V[MAX_N] = {0,};
		CNum D[4];
		int Max = 0;

		cin >> n >> m;

		if( n == 0 && m == 0 ){
			break;
		}

		for(int i = 0; i < n; i++){
			cin >> V[i];
		}

		sort(V, V + n);
		reverse(V, V + n);

		for(int i = 0; i < n; i++){
			for(int j = 0; j < 4; j++){
				if( j == 0 ){
					D[j].v.push_back( V[i] );
				}else if( j > 0 ){
					D[j].v.clear();
					for(unsigned int l = 0; l < D[j - 1].v.size(); l++ ){
						int tmp = V[i] + D[j - 1].v[l];
						if( tmp <= m ){
							int index = -1;
							
							for(int k = 0; k < n; k++){
								if( tmp + V[k] <= m ){
									index = k;
									break;
								}
							}
							if( index >= 0 && tmp + V[index] >= Max && !Find(D[j].v, tmp) ){
								if( Max < tmp ){
									Max = tmp;
								}
								D[j].v.push_back( tmp );
							}
						}
					}
				}
			}
		}

		for(int i = 0; i < 4; i++){
			//cout << " " << j << ":" << '\n';
			for(unsigned int j = 0; j < D[i].v.size(); j++ ){
				//cout << D[j].v[k] << ' ';
				int tmp = D[i].v[j];
				if( tmp <= m && Max < tmp){
					Max = tmp;
				}
			}
			//cout << '\n';
		}
		cout << Max << '\n';
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<cstdlib>
#include<typeinfo>
#include<sstream>
#include<list>
#include<set>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define fori(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
int in()
{
	int x;cin>>x;
	return x;
}
int stringInt(string a)
{
	return atoi(a.c_str());
}
string intString(int a)
{
	stringstream ss;
	ss<<a;
	return ss.str();
}
vector<int> SieveOfEratosthenes(int x)
{
	vector<int> a;
	vector<int> b(x+1,0);
	for(int i=2;i<=x;i++)
	{
		if(b[i]==0)
		{
			a.push_back(i);
			for(int l=i;l<=x;l+=i)
			{
				b[l]=1;
			}
		}
	}
	return a;
}

int main ()
{
	int n,m;
	while(cin>>n>>m&&n&&m)
	{
		vector<long long > a,b;
		b.push_back(0);
		rep(i,n)
		{
			b.push_back(in());
		}
		rep(i,n+1)
		{
			rep(l,n+1)
			{
				a.push_back(b[i]+b[l]);
			}
		}
		long long ans = 0;
		sort(a.begin(),a.end());
		rep(i,a.size())
		{
			vector<long long>::iterator t=upper_bound(a.begin(),a.end(),m-a[i]);
			if(t==a.begin())continue;
			t--;
			ans=max(ans,*t+a[i]);
		}
		cout<<ans<<endl;


	}

}