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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;
 
#define int long long 
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair
 
static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;
 
template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
    v.assign(a,vector<T>(b,t));
}
 
//v.erase(unique(v.begin(),v.end()),v.end());

signed main(){
	int n;
	int m;
	while(cin>>n>>m,n||m){
		// int n;
		// cin>>n;
		// int m;
		// cin>>m;
		vint v(n);
		rep(i,n){
			cin>>v[i];
		}
		v.PB(0);
		vint tmp;
		rep(i,n+1){
			rep(j,n+1){
				tmp.PB(v[i]+v[j]);
			}
		}
		sort(ALL(tmp));
		int ans=0;
		tmp.erase(unique(ALL(tmp)),tmp.end());
		rep(i,tmp.size()){
			rep(j,tmp.size()){
				if(tmp[i]+tmp[j]<=m){
					ans=max(ans,tmp[i]+tmp[j]);
				}
			}
		}
		cout<<ans<<endl;
	}
}

C++: #include <iostream>
#include <cstring>
using namespace std;
int n;
long long m,ans;
long long point[1000];
int main(void){
    cin>>n>>m;
    while(n!=0||m!=0){
        for(int i=0;i<n;i++){
            cin>>point[i];
        }
        
        ans=0;
        
        for(int i=0;i<n-1;i++){
            for(int j=i;j<n;j++){
                if(point[i]<point[j]){
                    int change;
                    change=point[i];
                    point[i]=point[j];
                    point[j]=change;
                }
            }
        }
        
        int darts;
        darts=0;
        
        for(int i=0;i<n;i++){
            if(darts==4||ans==m){break;}
            if(ans+point[i]<=m){
                //cout<<point[i]<<endl;
                ans+=point[i];
                i--; darts++;
            }
        }
        cout<<ans<<endl;
        cin>>n>>m;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <set>
#define N_MAX 1001

using namespace std;

int N, M;
int P[N_MAX];


void solve() {
  set<int> S;
  sort(P, P+N+1);
  for(int i = 0; i <= N; ++i) {
    for(int j = i; j <= N; ++j) {
      for(int k = j; k <= N; ++k) {
	int tmp = P[i] + P[j] + P[k];
	if(tmp > M) continue;
	S.insert(tmp);
      }
    }
  }

  for(int eps = 0; ; ++eps) {
    for(set<int>::iterator ii = S.begin(); ii != S.end(); ++ii) {
      int tmp = M - *ii - eps;
      if(tmp < 0) continue;
      if(binary_search(P, P+N+1, tmp)) {
	cout << *ii + tmp << endl;
	return;
      }
    }
  }
}

int main() {
  while((cin >> N >> M) && N && M) {
    P[0] = 0;
    for(int i = 1; i <= N; i++) {
      cin >> P[i];
    }
    solve();
  }
  return 0;
}

C++: //Bokan ga bokka--nn!!
//Daily Lunch Special Tanoshii !!
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d",&x)
#define rep(i,x) for(int i=0;i<x;i++)
int main()
{
	int n,m;
	while(1)
	{
		scanf("%d %d",&n,&m); if(!n) return 0;
		vector<int>val(0);
		vector<int>pos(0);
		int ret=0;
		for(int i=0;i<n;i++)
		{
			int f; scanf("%d",&f);
			val.pb(f);
		}
		val.pb(0);
		n++;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				pos.pb(val[i]+val[j]);
			}
		}
		sort(pos.begin(),pos.end());
		pos.erase(unique(pos.begin(),pos.end()),pos.end());
		for(int i=0;i<pos.size();i++)
		{
			int v=upper_bound(pos.begin(),pos.end(),m-pos[i])-pos.begin();
			if(!v) continue;
			ret=max(ret,pos[i]+pos[v-1]);
		}
		printf("%d\n",ret);
		
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1002002];
int main(void){
	while(1){
		ll n,m,i,j,p[1001],hi,lo,mi,pp,x,mx;
		cin>>n>>m;
		if(n==0)break;
		p[0]=0;
		for(i=1;i<=n;i++) cin>>p[i];
		pp=pow(n+1,2);
		for(i=0;i<=pp+1;i++) a[i]=0;
		for(i=0;i<=n;i++) for(j=0;j<=n;j++) a[i*n+j]=p[i]+p[j];
		sort(a,a+(pp+1));
//		for(i=0;i<=pp+1;i++) cout<<a[i]<<endl;
		mx=0;
		for(i=0;i<=pp+1;i++){
			hi=pp+1; lo=0;
			while(hi>=lo){
				mi=(hi+lo)/2;
				x=a[i]+a[mi];
				if(x>m) hi=mi-1;
				else if(x<m) lo=mi+1;
			}
			x=a[i]+a[hi];
			if(mx<x&&m>=x) mx=x;
		}
		cout<<mx<<endl;
	}
	return 0;
}

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;
		for_(i,0,N) {
			int x;
			cin >> x;
			if (x <= M) P.push_back(x);
		}
		
		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) ans = max(ans, *(upper_bound(P.begin(), P.end(), M - P[i]) - 1) + P[i]);
		cout << ans << endl;
	}
}

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

using namespace std;

#define pb push_back

int main() {
  
  int n, m;
  
  while(cin >> n >> m && (n|m)) {
    vector<int> data;
    vector<int> Q;
    
    n++;
    data.pb(0);
    for(int i=1; i<n; i++) {
      int a;
      cin >> a; data.pb(a);
    }
    
    for(int i=0; i<n; i++) {
      Q.pb(data[i]);
    }
    
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
	Q.pb(data[i] + data[j]);
      }
    }
    
    sort(Q.begin(), Q.end());

    // for(int i=0; i<T; i++) cout << Q[i] << endl;
    
    int ans = 0;
    
    for(int i=0; i<Q.size(); i++) {
      int M = m - Q[i];
      if(M < 0) break;
      
      int lb = 0, ub = Q.size();
      
      while(ub - lb > 1) {
	int mid = (lb + ub) / 2;
	
	if(Q[mid] <= M) {
	  lb = mid;
	} else {
	  ub = mid;
	}
      }
      ans = max(ans, Q[i]+Q[lb]);
    }
    
    cout << ans << endl;
  }
  
  return 0;
}