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 <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;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	int N, M;
	while(cin >> N >> M, N) {
		vi p(N);
		REP(i, N) {
			cin >> p[i];
		}
		p.push_back(0);

		vi pts;
		REP(i, N+1) {
			REP(j, N+1) {
				pts.push_back(p[i]+p[j]);
			}
		}
		sort(ALL(pts));

		int size = pts.size(), l = 0, r = size-1, ans = 0;
		while(l < size) {
			if(r < 0) {
				break;
			}
			if(pts[l]+pts[r] > M) {
				r--;
			} else {
				ans = max(ans, pts[l]+pts[r]);
				l++;
			}
		}

		cout << ans << endl;
	}

	return 0;
}

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

int main()
{
while(true) {
int n, m;
vector<int> x, xxx;
scanf("%d%d", &n, &m);
if(n == 0)
break;
x.resize(n);
for(int i = 0; i < n; ++i)
scanf("%d", &x[i]);
for(int i = 0; i < n; ++i) {
xxx.push_back(x[i]);
for(int j = 0; j < n; ++j)
xxx.push_back(x[i] + x[j]);
}
xxx.push_back(0);
sort(xxx.begin(), xxx.end());
int ans = 0;
for(int i = 0; i < xxx.size(); ++i) {
int r = m - xxx[i];
if(r < 0)
continue;
int idx = upper_bound(xxx.begin(), xxx.end(), r) - xxx.begin() - 1;
if(idx < 0)
continue;
ans = max(ans, xxx[i] + xxx[idx]);
}
printf("%d\n", ans);
}
return 0;
}