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>
#define PB push_back
using namespace std;
typedef vector<int>V;
int main(void){
	int n,m;
	int sum;
	int i,j;
	int low,high,mid,flg,key;
	int box[1001];
	V v;
	int MAX;
	while(1){
		cin>>n>>m;
		if(n==0&&m==0)	break;
		v.clear();
		box[0]=0;
		for(i=1;i<=n;i++)	cin>>box[i];
		sort(box,box+n+1);
		for(i=0;i<=n;i++){
			for(j=i;j<=n;j++){
				if(box[i]+box[j]<=m)	v.PB(box[i]+box[j]);
			}
		}
		sort(v.begin(),v.end());
		//for(i=0;i<v.size();i++)	cout<<v[i]<<endl;
		MAX=-1;
		for(i=0;i<v.size();i++){
			sum=v[i];
			if(sum<=m){
				key=m-sum;
				//cout<<key<<endl;
				flg=0;
				low=0;
				high=v.size()-1;
				while(low<=high){
					mid=(low+high)/2;
					if(key<v[mid]){
						high=mid-1;
					}
					else if(key>v[mid]){
						low=mid+1;
					}
					else{
						flg=1;
						break;
					}
				}
				if(flg==1){
					sum+=key;
				}
				else{
					sum+=v[high];
				}
				//cout<<sum<<endl;
				MAX=max(MAX,sum);
			}
		}
		cout<<MAX<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) if(DEBUG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DEBUG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
const bool DEBUG = false;

signed main() {
	int P[1010] = {};
	static int a[2000000] = {}; //1000010
	for (int N, M; cin >> N >> M&&N;) {
		memset(a, 0x3f, sizeof(a));
		rep(i, 0, N)cin >> P[i];
		P[N] = 0;
		N++;
		rep(i, 0, N)rep(j, 0, N) {
			a[i*N + j] = P[i] + P[j];
		}
		dumpc(a);
		sort(a, a + N*N);
		dumpc(a);
		int ans = 0;
		rep(i, 0, N*N) {
			if (a[i] > M)break;
			int add = a[upper_bound(a, a + N*N, M - a[i]) - a - 1];
			ans = max(ans, a[i] + add);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int sum[1002100];

int main(){
  int n,d;
  int m[1111];
  while(cin >> n >> d , n){
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=n;i++){
      cin >> m[i];
    }
    m[0] = 0;
    n++;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	sum[i*n+j] = m[i] + m[j];
      }
    }
    sort(sum,sum+n*n);
    
    int res = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	int ab = m[i] + m[j];
	if(ab <= d){
	  int h = d - ab;
	  int tmp = sum[upper_bound(sum,sum+n*n,h)-sum-1] + ab;
	  if(tmp <= d){
	    res = max(res,tmp);
	  }
	}
      }
    }
    cout << res << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

int solve(int n, int m){
	int p[n];
	REP(i, n) cin >> p[i];
	SORT(p, n);
	
	int res=0;
	REP(i, n){
		while(res<=m){
			res+=p[i];
		}
		res-=p[i];
	}
	
	cout << res << endl;
}

int main(){
	while(1){
		int n, m;
		cin >> n >> m;
		if(n==0 && m==0) return 0;
		solve(n, m);
	}
}

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

using namespace std;

using ll = long long;
using ull = unsigned long long;

#define REP(i,n) for(long long i = 0; i < (n); i++)
#define FOR(i, m, n) for(long long i = (m);i < (n); ++i)
#define ALL(obj) (obj).begin(),(obj).end()

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;

const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ll LLINF = (ll)1e18;
const ll INTINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;

template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max<T>(a,b);}
template <class T> void chmin(T& a, const T b){a=min<T>(a,b);}
void YN(bool flg) {cout << ((flg) ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << ((flg) ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << ((flg) ? "yes" : "no") << endl;}

int main() {
	while(1){
		ll N,M; cin >> N >> M;
		if(N==0 && M == 0) return 0;
		V<ll> P(N+1,0), X;
		for(int i = 1; i <= N; ++i) cin >> P[i];
		for(int i = 0; i <= N; ++i) for(int j = i; j <= N; ++j)	X.push_back(P[i]+P[j]);
		sort(ALL(X));
		ll ans = 0;
		for(auto e:X){
			if(e>M) continue;
			ll c = *--upper_bound(ALL(X),M-e);
			chmax(ans,c+e);
		}
		cout << ans << endl;
	}

	return 0;
}