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+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 <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> *points;
vector<int> *twice;

void deldup(vector<int> *vec) {
    vector<int>::iterator it = vec->begin();
    bool is_straight = false;
    int save = -1;

    while (it != vec->end()) {
        if (*it == save) {
            vec->erase(it++);
            is_straight = true;
            continue;
        } else {
            save = *it;
            if (is_straight) {
                vec->erase(it++);
                is_straight = false;
                continue;
            }
            is_straight = false;
        }
        it++;
    }
}

int search(vector<int> *target, int standard) {
    int ind;
    int min = 0;
    int max = target->size() - 1;

    while (1) {
        ind = (min + max)/2;

        if (min + 1 == max) return target->at(min);

        if (target->at(ind) <= standard) {
            min = ind;
        } else {
            max = ind;
        }
    }
}

int main() {
    while (1) {
        int i;
        int n, m;
        int max = 0;

        scanf("%d %d", &n, &m);
        if (n == 0 && m == 0) return 0;

        points = new vector<int>();
        twice = new vector<int>();

        points->push_back(0);
        for (i=0;i<n;i++) {
            int k;
            scanf("%d", &k);
            points->push_back(k);
        }

        vector<int>::iterator it = points->begin();
        while (it != points->end()) {
            vector<int>::iterator it2 = points->begin();
            while (it2 != points->end()) {
                twice->push_back(*it + *it2);
                it2++;
            }
            it++;
        }

        sort(twice->begin(), twice->end());
        deldup(twice);

        vector<int>::reverse_iterator it1 = twice->rbegin();
        while (it1 != twice->rend()) {
            if (*it1 <= m) {
                int got = *it1 + search(twice, m - *it1);
                if (max < got) max = got;
            }
            it1++;
        }

        printf("%d\n", max);
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#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 n;
llong m;

llong bs(vector<llong> v, llong t){
	int l=0, r=v.size()-1;
	int mid;
	while(l<r){
		mid=(l+r)/2;
		if(v[mid]>t) r=mid;
		else if(v[mid]<t){
			if(v[mid+1]>t) return v[mid];
			else l=mid+1;
		}
		else break;
	}
	
	return v[r-1];
}

llong solve(vector<llong> p){
	vector<llong> v;
	set<llong> st;
	int c=0;
	REP(i, n){
		REPR(j, n-1){
			if(st.find(p[i]+p[j])==st.end()){
				v.push_back(p[i]+p[j]);
				st.insert(v[c]);
				c++;
			}
		}
	}
	VSORT(v);
	
	llong res=0;
	REP(i, c){
		if(v[i]>m) continue;
		res=max(res, v[i]+bs(v, m-v[i]));
	}
	return res;
}

int main(){
	while(1){
		scanf("%d%lld", &n, &m);
		if(n==0 && m==0) return 0;
		n++;
		vector<llong> p(n);
		p[0]=0;
		FOR(i, 1, n) scanf("%lld", &p[i]);
		VSORT(p);
		llong ans=solve(p);
		printf("%lld\n", ans);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n,d;
  int m[1111];
  while(cin >> n >> d , n){
    int sum[n*n+10];
    memset(sum,0,sizeof(sum));
    for(int i=0;i<n;i++){
      cin >> m[i];
    }
    m[n] = 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];
	int h = d - (m[i] + m[j]);
	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<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[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];
		}
		sort(a, a + N*N);

		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 <cstdio>
#include <algorithm>
using namespace std;

int p[1010];
int sum[1002010];

int main(void){
	int n,m;

	while(scanf("%d%d",&n,&m) && (n||m)){
		for(int i=0;i<n;i++){
			scanf("%d",&p[i]);
		}
		p[n++] = 0; //0ツ点ツづ個スツコツアツづーツ催ャツづゥ

		int size=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				sum[size++] = p[i] + p[j];
			}
		}
		sort(sum,sum+size);

		int ans = 0;

		for(int i=0;i<size;i++){
			int rem = m - sum[i];
			if(rem < 0) continue;
			int left = 0, right = size;

			while(left < right){
				int center = (left + right) / 2;
				if(rem > sum[center]){
					left = center + 1;
				}
				else if(rem < sum[center]){
					right = center;
				}
				else {
					break;
				}
			}

			ans = max(ans, sum[i] + sum[right-1]);
		}

		printf("%d\n",ans);
	}

	return 0;
}

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

using namespace std;

int main(void)
{
   vector<int>a;
   priority_queue<int>ans;
   a.push_back(0);

   int n,m;
   int input;
   scanf("%d%d",&n,&m);
   for(int i = 0;i < n;i++)
   {
      scanf("%d",&input);
      a.push_back(input);
   }

   int s = a.size();
   int one,two;
   for(int i = 0;i < s;i++)
      for(int j = 0;j < s;j++)
      {
         one = a[i],two = a[j];
         if(one + two <= m || one+two == 0) a.push_back(one + two);
      }

   s = a.size();
   for(int i = 0;i < s;i++)
      for(int j = 0;j < s;j++)
      {
         one = a[i],two = a[j];
         if(one+two <= m) ans.push(one+two);
      }
   cout<<ans.top()<<endl;

   return 0;
}