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>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD=1e9+7;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n,m;
  while(true){
      cin>>n>>m;
      if(n==0&&m==0) break;
      std::vector<ll> v(n),kouho;
      for(int i=0;i<n;i++) cin>>v[i];
      v.push_back(0);
      for(int i=0;i<=n;i++)for(int j=0;j<=n;j++){
          kouho.push_back(v[i]+v[j]);
      }
      ll ma=-1;
      sort(kouho.begin(),kouho.end());
      for(int i=0;i<(int)kouho.size();i++){
          if(i>0&&kouho[i]==kouho[i-1]) continue;
          ll res=m-kouho[i];
          if(res<0) break;
          auto itr=upper_bound(kouho.begin(),kouho.end(),res);
          itr--;
          ma=max(kouho[i]+*itr,ma);
          //cout<<ma<<endl;
      }
      cout<<ma<<endl;
  }
  return 0;
}

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

	int tsP[1001 * 1001];
	int main() {
		int N, M;
		int P[1001];
		int ans;
		int cnt;

		P[0] = 0;

		//cin >> N >> M;
		while (cin>>N>>M,N&&M) {
			ans = 0;
			cnt = 0;
			for (int i = 1; i <= N; i++) {
				cin >> P[i];
			}

			for (int i = 0; i <= N; i++) {
				for (int j = i; j <= N; j++) {
					if (P[i] + P[j] <= M) {
						tsP[cnt++] = P[i] + P[j];
					}

				}
			}
			sort(tsP, tsP + cnt);
			for (int i = 0; i < cnt; i++) {
				//for (int j = i; j < cnt; j++) {
					//if (tsP[i] + tsP[j] <= M) {
						ans = max(ans, /*tsP[i] + tsP[j]*/tsP[i] + *(upper_bound(tsP, tsP + cnt, M - tsP[i]) - 1));
					//}
				//}
			}
			cout << ans << endl;
		}
		return 0;
	}

C++: #include <iostream>
#include <algorithm>
#define N1 1001
using namespace std;
int N,M,P[N1],data[N1][N1][3];
int main(){
  int i,j,maxv,k;
  while(1){
    cin >> N >> M;
    if(N==0&&M==0) break;
    for(i=0;i<N;i++) cin >> P[i];
    for(i=0;i<N;i++){
      for(j=i;j<N;j++){
	data[i][j][0]=P[i]+P[j];
	data[i][j][1]=P[i]+P[j]*2;
	data[i][j][2]=P[i]+P[j]*3;
      }
    }
    maxv=0;
    for(i=0;i<N;i++){
      for(j=i;j<N;j++){
	for(k=0;k<3;k++){
	  if(data[i][j][k]<=M) maxv=max(maxv,data[i][j][k]);
	}
      }
    }
    cout << maxv << endl;
  }
  return 0;
}

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

int main()
{
  int N, M;
  while (cin >> N >> M && N != 0) {
    vector<int> ps(N);
    for (int i = 0; i < N; i++) {
      cin >> ps[i];
    }

    vector<int> v(1, 0);
    for (int i = 0; i < N; i++) {
      v.push_back(ps[i]);
      for (int j = i; j < N; j++) {
        if (ps[i] + ps[j] <= M) {
          v.push_back(ps[i] + ps[j]);
        }
      }
    }
    sort(v.begin(), v.end());

    int ans = 0;
    for (vector<int>::const_iterator it(v.begin()); it != v.end(); ++it) {
      vector<int>::const_iterator jt = lower_bound(v.begin(), v.end(), M - *it);
      if (jt != v.begin()) {
        --jt;
        ans = max(ans, *it + *jt);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main() {

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]);
		}
	}
	for(ll i=M;i>=0;i--){
		for(set<ll>::iterator it=S.begin();it!=S.end();it++){
			if(i-(*it) < 0)break;
			if(S.find(i-(*it))!=S.end()){
				return i;
			}
		}
	}
	return 0;
}

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

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

int main(int argc, char *argv[]){
  int n, m;
  while(cin >> n >> m, n){
    int point[n + 1];
    point[0] = 0;
    set<int> a, b;
    for (int i = 1; i <= n; i++) {
      cin >> point[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a.insert(point[i] + point[j]);
      }
    }
    int ans = 0;
    for (int i: a) {
      if(--a.upper_bound(m - i) != a.end()){
        ans = max(ans, i + *(--a.upper_bound(m - i)));
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD (1000000007)
#define INF (1e9)
#define INFL (1e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} pr(a[a.size()-1]);}
template<class T>void prarr(arr<T>& a){rep(i, a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int n, m;
vector<int> a;

void solve(){
    vector<int> c(n*(n-1)/2 + n + 1);
    int ci=0;
    rep(i, n) FOR(j, i+1, n){
        c[ci++] = a[i] + a[j];
    }
    rep(i, n) c[ci++] = a[i];
    c[ci] = 0;
    sort(ALL(c));
    // prvec(c);
    int ans = 0;
    rep(i, n) rep(j, n){
        if(a[i] + a[j] > m) continue;
        // printf("%d, %d, %d\n", a[i], a[j], m - a[i] - a[j]);
        int k = *(--upper_bound(ALL(c), m - a[i] - a[j]));
        // printf("k=%d\n", k);
        // pr(a[i] + a[j] + k);
        chmax(ans, a[i] + a[j] + k);
    }
    pr(ans);
}

int main()
{
    while(cin >> n >> m){
        if(n==0 && m==0) break;
        a.resize(n);
        rep(i, n) cin >> a[i];
        solve();
    }
    return 0;}