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 <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int main() {
	int N, M;
	
	while (cin >> N >> M, N) {
		vector< int > P(N);
		for_(i,0,N) cin >> P[i];
		
		P.push_back(0);
		for_(i,0,N) for_(j,0,N) if (P[i] + P[j] <= M) P.push_back(P[i] + P[j]);
		sort(P.begin(), P.end());
		
		int m = P.size(), ans = 0;
		for_(i,0,m) {
			int x = P[i];
			int y = *(upper_bound(P.begin(), P.end(), M - x) - 1);
			ans = max(ans, x + y);
		}
		
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef long long INT;
typedef vector <INT> VI;
typedef set <INT> SI;

INT P[1001];
INT Q[1002001];

INT solve( int N, INT M ) {
    INT answer = 0;
    for ( int i = 0; i < N; ++ i ) {
        int q = Q[i];
        int L = 0;
        int R = N;
        INT m = M - q;
        while ( L < R ) {
            int p = ( R - L ) / 2 + L;
            INT t = q + Q[p];
            if ( t > M ) t = 0;
            answer = max( answer, t );
            if ( Q[p] > m ) {
                R = p - 1;
            } else {
                L = p + 1;
            }
        }
    }
    return answer;
}

int main() {
    int n, m;
    while ( cin >> n >> m ) {
        if ( n == 0 && m == 0 ) break;
        for ( int i = 0; i < n; ++ i ) {
            INT p;
            cin >> p;
            P[i] = p;
        }
        P[n] = 0;
        for ( int i = 0; i < n + 1; ++ i ) {
            for ( int j = 0; j < n + 1; ++ j ) {
                Q[i * ( n + 1 ) + j] = P[i] + P[j];
            }
        }
        int length = ( n + 1 ) * ( n + 1 );
        sort( Q, Q + length );
        cout << solve( length, m ) << endl;
    }
    return 0;
}

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

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 1005
#define LSIZE 1000005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;
ll M;
ll T[SIZE];
set<ll> S;
ll K;

ll solve()
{
	scanf("%d %lld",&n,&M);
	if(n+M ==0)return -1;
	for(int i=1;i<=n;i++)scanf("%lld",&T[i]);
	T[0]=0;
	S.clear();
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			S.insert(T[i]+T[j]);
		}
	}
	ll maxv=0;
	set<ll>::iterator ii;
	for(set<ll>::iterator it=S.begin();it!=S.end();it++){
		for(set<ll>::iterator itt=S.begin();itt!=S.end();itt++){
			if((*it)+(*itt) <= M){
				maxv = max(maxv,(*it)+(*itt));
			}
		}
	}
	return maxv;
}

int main()
{
	while(1){
		ll ans = solve();
		if(ans==-1)break;
		else printf("%lld\n",ans);
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
 
int n, m;
vector<int> p1, p2;
int main()
{
  while(cin >> n >> m && n && m){
    p1.clear();
    p2.clear();
     
    //chap1
    p1.push_back(0);
    for(int i = 0; i < n; i++){
      int p;
      cin >> p;
      p1.push_back(p);
    }
 
    //chap2
    for(int i = 0; i <= n; i++)
      for(int j = i; j <= n; j++)
    p2.push_back(p1[i] + p1[j]);
 
    sort(p2.begin(), p2.begin() + p2.size());
 
    //chap3
    int ans = 0;
    for(int i = 0; i < p2.size(); i++){
      vector<int>::iterator count2 = upper_bound(p2.begin(), p2.end(), m - p2[i]);
      if(count2 != p2.begin())
    ans = max(ans, *(count2 - 1) + p2[i]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

int main(void){
	while(1){
		int n, m; cin >> n >> m;
		if(n == 0 && m == 0) return 0;
		vector<int> p(n);
		rep(i, n) cin >> p[i];
		p.push_back(0);//??¢????????????????????\??????
		vector<int> pp((n + 1) * (n + 1));
		int cnt = 0;
		rep(i, n + 1)rep(j, n + 1){
			pp[cnt] = p[i] + p[j];
			cnt++;
		}
	
		sort(pp.begin(), pp.end());
		int ans = 0;
		//?¨???????(n^2*log(n*n))
		rep(i, (n + 1) * (n + 1)){
			int tmp = pp[i];
			int aim = m - tmp;
			if(aim < 0) continue;
			tmp += *(upper_bound(pp.begin(), pp.end(), aim) - 1);
			if(tmp <= m) ans = max(ans, tmp);
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
	vector<int> sum;
	int n,m;
	int a[1000];
	while(cin>>n>>m){
		int ans=0;
		a[0]=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a,a+n+1);
		for(int i=0;i<=n;i++){
			for(int j=i;j<=n;j++){
				if(m>=a[i]+a[j])sum.push_back(a[i]+a[j]);
			}
		}
		sort(sum.begin(),sum.end());
		for(int i=0;i<sum.size();i++){
			for(int j=i;j<sum.size();j++){
				if(sum[i]+sum[j]<=m){
					ans=max(ans,sum[i]+sum[j]);
				}
			}
		}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define min_3(a,b,c) min(a,min(b,c))
#define max_3(a,b,c) max(a,max(b,c))
#define mp1(a,b,c) P1(a,P(b,c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a,vector<a>,greater<a>>

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
const int kaijou[10]={1,1,2,6,24,120,720,5040,40320,362880};

int main(){
	while(1){
		int n,m;
		int p[1002];
		
		scanf("%d%d",&n,&m); if(n == 0 && m == 0)break;
		rep(i,n){
			scanf("%d",&p[i]);
		}
		
		vector<int> p1;
		rep(i,n){
			p1.pb(p[i]);
		}
		p1.pb(0);
		
		vector<int> p2;
		rep(i,n+1){
			rep(j,n+1){
				p2.pb(p1[i]+p1[j]);
			}
		}
		sor(p2);
		uniq(p2);
		
		int ret = 0;
		rep(i,p2.size()){
			if(p2[i] <= m){
				ret = max ( ret , p2[i] + *--ub(p2,m-p2[i]) );
			}
		}
		
		printf("%d\n",ret);
	}
}