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;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL<<61;

int p[1005], n, m, nn;
vector<int> gedo;

inline void cut(int *k) {
  if (m < *k) *k = 0;
}

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

  while (cin >> n >> m, n) {
    gedo.clear();
    for (int i = 0; i < n; ++i) {
      cin >> p[i];
    }

    for (int i = 0; i < n; ++i) {
      for (int j = i; j < n; ++j) {
        gedo.PB(p[i] + p[j]);
      }

      gedo.PB(p[i]);
    }
    gedo.PB(0);
    sort(gedo.begin(), gedo.end());
    gedo.PB(INF);

    int ans = 0;
    for (int i = 0; i < gedo.size() - 1; ++i) {
      int l = 0, r = gedo.size() - 1, cur = gedo[i];

      while (1 <= r - l) {
        int j = (r + l) / 2, temp1 = cur + gedo[j], temp2 = cur + gedo[j + 1];
        cut(&temp1);
        cut(&temp2);
        ans = max(ans, temp1);

        if (temp1 != 0 && temp2 == 0) break;

        if (temp1 == 0) {
          r = j;
        } else {
          l = j + 1;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

int main(){
while(1){
	int n,m,a,ans=0;
	vector<int> p,pp;
	scanf("%d%d",&n,&m);
	p.push_back(0);
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		p.push_back(a);
	}
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			pp.push_back(p[i]+p[j]);
		}
	}
	sort(pp.begin(),pp.end());
	int s=pp.size()-1;
	for(int i=0;i<=s;i++){
		while(pp[i]+pp[s]>m)s--;
		if(ans<pp[i]+pp[s])ans=pp[i]+pp[s];
	}
	printf("%d\n",ans);
}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589


#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*) array, (T*) (array + N), val);
}
pii Dir[8] = { //?§????
	{ 0 ,1 },{ -1 ,0 },{ 1 ,0 },{ 0 ,-1 },
	{ 1 ,1 },{ 1 ,-1 },{ -1 ,1 },{ -1 ,-1 }
};

signed main() {
	int n, m;

	while (scanf("%d %d", &n, &m)) {
		if (!n)
			break;
		vector<int> po(n);
		REP(i, n) {
			scnaf("%d", &po[i]);
		}
		po.push_back(0);
		set<int> toku;
		REP(i, n + 1) {
			REP(j, n + 1) {
				toku.insert(po[i] + po[j]);
			}
		}
		int ans = 0;
		for (auto itr : toku) {
			auto itr2 = --toku.upper_bound(m - itr);
			if (itr2 != toku.end())
				ans = max(itr + *itr2, ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

using namespace std;

int main() {
	int n, m;

	while (cin >> n >> m, n || m) {
		vector<int> point(n + 1);
		vector<int> sum;
		for (int i = 0; i < n; ++i) {
			cin >> point[i];
		}
		for (int i = 0; i < n + 1; ++i) {
			for (int j = i; j < n + 1; ++j) {
				sum.push_back(point[i] + point[j]);
			}
		}
		sort(sum.begin(), sum.end());

		int ans = 0;
		for (int i = 0; i < sum.size(); ++i) {
			int t = m - sum[i];
			cout << "t = " << t << endl;
			vector<int>::iterator it = upper_bound(sum.begin(), sum.end(), t);
			if (it != sum.begin()) {
				ans = max(ans, sum[i] + * (it - 1));
			}
		}
		cout << ans << endl;
	}
}

C++: 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	int ans[4];
	int temp;
	double energy,energy_min,energy_now;
	int M, N;
	int* p;
	int i, j;
	double T=100;
	int ret;
	while (1) {
		scanf("%d%d", &N, &M);
		p = (int*)malloc((N+1) * sizeof(int));
		for (i = 0; i < N; i++) {
			scanf("%d", &p[i]);
		}
		p[N] = 0;
		if (N == M && N == 0) {
			break;
		}
		energy_min = pow(10, 300);
		for (j = 0; j < 4; j++) {
			ans[j] = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
		}
		
		for (T = 1000001; T > 103; T--) {
			for (j = 0; j < 4; j++) {
				temp = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
				for (j = 0; j < 4; j++) {
					ret = ans[0] + ans[1] + ans[2] + ans[3];
					ret += temp - ans[j];
					energy = M - ret;
					if (M < ret)energy += 100000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*10)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}

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

int N, M;
vector<int> point;
vector<int> sel_two;

int find_max_under(int n) {
	int ub = sel_two.size()-1;
	int lb = 0;
	int mid = (lb + ub) / 2;

	while (!(lb == ub-1)) {
		mid = (lb + ub) / 2;
		if (sel_two[mid] < n) {
			lb = mid;
		} else if (sel_two[mid] > n) {
			ub = mid;
		} else {
			return n;
		}
	}
	return sel_two[lb];
}

int main() {
	while (cin >> N >> M, N || M) {
		point.clear(); point.resize(N+1);
		sel_two.clear();
		point[0] = 0;
		for (int i = 1; i <= N; i++) cin >> point[i];
		
		int max_point = 0;
		for (int i = 0; i <= N; i++) 
			for (int j = 0; j <= N; j++) 
				if (point[i] + point[j] <= M) sel_two.push_back(point[i] + point[j]);
		sort(sel_two.begin(), sel_two.end());
		for (int i = 0; i < sel_two.size(); i++) {
			int max_under = find_max_under(M - sel_two[i]);
			max_point = max(sel_two[i] + max_under, max_point);
		}
//		for (int i = 0; i < sel_two.size(); i++) cout << sel_two[i] << ' ';
//		cout << endl;

		cout << max_point << endl;
	}
	return 0;
}