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 _, val := range sums {
			rem := M - val
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, val+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

C++: #include<bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

typedef std::pair<int,int> PII;


int N, M;
VI P(1000);
VI sum(1000000);

int main() {
    while(std::cin >> N >> M) {
        if(N == 0 && M == 0) break;
        REP(i, N) {
            std::cin >> P[i];
        }

        int cnt = 0;
        REP(i, N) REP(j, N) {
            if(P[i] + P[j] > M) continue;
            sum[cnt++] = P[i] + P[j];
        }
        std::sort(sum.begin(), sum.begin() + cnt);

        int ans = 0;
        for(int i=0; i<cnt; ++i) {
            int v2 = *(std::upper_bound(sum.begin(), sum.begin()+cnt, M - sum[i]) - 1);
            ans = std::max(ans, sum[i] + v2);
        }
        std::cout << ans << std::endl;
    }
}

C++: /* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll

/* ---------- Constants  */
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
signed main() {
    while (1) {
        int N, M;
        cin >> N >> M;
        if (!(N || M)) break;
        vector<int> A(N + 1, 0);
        for (int i = 0; i < N; i++) cin >> A[i];

        vector<int> pool;
        for (int i = 0; i <= N; i++) {
            for (int j = i; j <= N; j++) {
                pool.push_back(A[i] + A[j]);
            }
        }
        sort(pool.begin(), pool.end());

        int ret = 0;
        for (int i = 0; i < pool.size(); i++) {
            int remain = M - pool[i];
            auto it = upper_bound(pool.begin(), pool.end(), remain);
            if (it == pool.begin()) continue;
            ret = max(pool[i] + *(it - 1), ret);
        }

        cout << ret << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#define N1 1001
using namespace std;
int Search(int);
int N,M,P[N1];
vector<int> V,cp,cp1;
int main(){
  int i,j,maxv,r;
  while(1){
    cin >> N >> M;
    if(N==0&&M==0) break;
    for(i=0;i<N;i++){
      cin >> P[i];
      V.push_back(P[i]);
    }
    V.push_back(0);
    for(i=0;i<N;i++){
      for(j=i;j<N;j++){
	V.push_back(P[i]+P[j]);
      }
    }
    cp=cp1=V;
    for(i=0;i<cp.size();i++){
      for(j=0;j<cp1.size();j++){
	V.push_back(cp[i]+cp1[j]);
      }
    }
    for(i=0;i<N;i++){
      for(j=0;j<cp.size();j++){
	V.push_back(P[i]+cp[j]);
      }
    }
    sort(V.begin(),V.end());
    maxv=0;
    for(i=0;i<V.size();i++){
      r=Search(V[i]);
      maxv=max(maxv,r);
    }
    cout << maxv << endl;
    V.clear();
    cp.clear();
    cp1.clear();
  }
  return 0;
}
int Search(int a){
  int left=0,right=V.size()-1,mid;
  while(1){
    //x    cout << left << ' ' << right << endl;
    mid=(right+left)/2;
    if(a+V[mid]<=M) left=mid+1;
    else if(a+V[mid]>M) right=mid;
    if(left==right) break;
  }
  return V[left-1];
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

int N, M;



int main() {
	ios::sync_with_stdio(false);
	cin >> N >> M;
	while (N) {
		int num[1001];
		int ans = 0;
		set<int>F;
		set<int>S;
		for (int i = 0; i < N; i++)cin >> num[i];
		num[N] = 0;
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= N; j++) {
				F.insert(num[i] + num[j]);
			}
		}
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= N; j++) {
				S.insert(num[i] + num[j]);
			}
		}
		set<int>::reverse_iterator ritr = S.rbegin();
		for (auto i : F) {
			if (ritr == S.rend())break;
			while ((i + *ritr) > M) {
				++ritr;
				if (ritr ==S.rend())break;
			}
			if(ritr!=S.rend())	ans = max(ans, i + *ritr);
		}
		cout << ans << endl;
		cin >> N >> M;
	}
	return 0;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

typedef vector<int> vi;

int main() {
	while (true) {
		int n, m, score[1001];
		vi v;
		cin >> n >> m;
		if (n == 0 && m == 0) break;
		REP(i, n) scanf("%d", &score[i]);
		score[n++] = 0;

		REP(i, n) FOR(j, i, n) if (score[i] + score[j] <= m)
			v.push_back(score[i] + score[j]);

		sort(v.begin(), v.end());
		unique(v.begin(), v.end());

		int ans = 0;
		REP(i, v.size()) {
			vi::iterator it = upper_bound(v.begin(), v.end(), m - v[i]);
			if (it != v.begin())
				ans = max(ans, *(it - 1) + v[i]);
		}

		cout << ans << endl;
	}
	return 0;
}