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<stdio.h>
#include<iostream>
#include<algorithm>
int main(){
while(1){
int i,n,j,m,s=0,l,k,o=0;
static int p[1001]={0},u[1001][1001]={0},v[1001*1001];
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++)scanf("%d",&p[i]);
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
if(u[i][j]==0&&u[j][i]==0){
v[o]=p[i]+p[j];
o++;
}
}
}
for(i=0;i<o;i++){
for(j=0;j<o;j++){
if(v[i]+v[j]<=m&&v[i]+v[j]>s)s=v[i]+v[j];
}
}
printf("%d\n",s);
}
return 0;
}

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

int main(){
  int n,m;
  int p[1001];
  for(;;){
    cin >> n >> m;
    if(!n && !m)break;

    for(int i=0;i<n;i++)cin >> p[i];
    p[n] = 0;

    set<int> s;
    for(int i=0;i<=n;i++)
      for(int j=0;j<=n;j++)s.insert(p[i] + p[j]);

    set<int>::iterator ite,hoge;
   
    int ans = 0;
    for(ite = s.begin();ite!=s.end();ite++){
      int tmp = *ite;
      hoge = upper_bound(s.begin(),s.end(),m-tmp);
      if(hoge == s.begin())break;
      else if(hoge == s.end())ans = max(ans,tmp);
      else ans = max(ans,tmp + (*(--hoge) ) );
    }

    cout << ans << endl;
  }
}

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

int main(){
  int N, M;
  int P[1001], SUM[1000001];
  P[0] = 0;
  while(cin >> N >> M , N){

    for(int i = 1 ; i <= N ; i++ ){
      cin >> P[i];
    }
    int cnt = 0;
    for(int i = 0 ; i <= N ; i++ ){
      for(int j = N ; j >= i ; j-- ){
        if(P[i] + P[j] > M) continue;
        SUM[cnt++] = P[i] + P[j];
      }
    }
    sort( SUM, SUM + cnt);

   int ret = 0;
    for(int i = 0 ; i < cnt ; i++ ){
      ret = max( ret, SUM[i] + *(upper_bound( SUM, SUM + cnt, M - SUM[i]) - 1));
    }
    cout << ret << endl;
  }
}

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		long long int b;
		set<long long int> ss;
		ss.insert(0);
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				ss.insert(b);
			}
		}
		set<long long int> ss2;
		for (set<long long int>::iterator ite = ss.begin(); ite != ss.end(); ite++) {
			ss2.insert(*(ite));
			for (set<long long int>::iterator ite2 = ite; ite2 != ss.end(); ite2++) {
				if (*(ite)+*(ite2) <= m) {
					ss2.insert(*(ite)+*(ite2));
				} else {
					break;
				}
			}
		}
		ss.clear();
		vector<long long int> data;
		for (set<long long int>::iterator ite = ss2.begin(); ite != ss2.end(); ite++) {
			data.push_back(*(ite));
		}
		//cout << 'a' << endl;
		long long int a = m;
		for (long long int i = 0; i < 4; i++) {
			vector<long long int>::iterator ite = upper_bound(data.begin(), data.end(), a);
			ite--;
			a -= *(ite);
		}
		cout << m-a << endl;
	}
	return 0;
}

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

vector<long long>F1;
vector<long long>F2;

long long n,m;
long long a[10000];
long long maxn;
long long l,r,M;

int main(){
	while(true){
		maxn=0;
		cin>>n>>m;
		if(n==0 && m==0){break;}
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		F1.clear();
		F2.clear();
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				F1.push_back(a[i]+a[j]);
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				F2.push_back(a[i]+a[j]);
			}
		}
		sort(F2.begin(),F2.end());
		for(int i=0;i<F1.size();i++){
			if(F1[i]<=m){
				l=0;r=F2.size();
				for(int j=0;j<21;j++){
					M=(l+r)/2;
					if(F1[i]+F2[M]>m){
						r=M;
					}
					else if(M==F1.size()-1){
						l=M;
					}
					else if(F1[i]+F2[M+1]<=m){
						l=M;
					}
				}
				maxn=max(maxn,F1[i]+F2[M]);
			}
		}
		cout<<maxn<<endl;
	}
	return 0;
}

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 = 1000088; T > 1000; 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*1000)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

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

C++: #include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
template <typename Iter, typename T> int gt(Iter L, Iter R, T v) { return upper_bound(L, R, v) - L; }
template <typename Iter, typename T> int ge(Iter L, Iter R, T v) { return lower_bound(L, R, v) - L; }
template <typename Iter, typename T> int lt(Iter L, Iter R, T v) { return lower_bound(L, R, v) - L - 1; }
template <typename Iter, typename T> int le(Iter L, Iter R, T v) { return upper_bound(L, R, v) - L - 1; }
template <typename Bst, typename T> auto gt(const Bst& bst, T v) { return bst.upper_bound(v); }
template <typename Bst, typename T> auto ge(const Bst& bst, T v) { return bst.lower_bound(v); }
template <typename Bst, typename T> auto lt(const Bst& bst, T v) { return bst.lower_bound(v) == bst.begin() ? bst.end() : --bst.lower_bound(v); }
template <typename Bst, typename T> auto le(const Bst& bst, T v) { return bst.upper_bound(v) == bst.begin() ? bst.end() : --bst.upper_bound(v); }
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    // ifstream cin("input.txt");
    // ofstream cout("output.txt");
    int n, m;
    while(cin >> n >> m and n and m) {
        vector<ll> p(n + 1);
        vector<ll> pairs = {0};
        for(int i = 1; i <= n; i++) {
            cin >> p[i];
        }
        sort(p.begin(), p.end());
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(p[i] + p[j] <= m) {
                    pairs.push_back(p[i] + p[j]);
                }
            }
        }
        sort(pairs.begin(), pairs.end());
        ll ans = max(p[le(p.begin(), p.end(), m)], pairs[le(pairs.begin(), pairs.end(), m)]);
        for(int i = 0; i < pairs.size() and pairs[i] <= m; i++) {
            ans = max(ans, pairs[i] + p[le(p.begin(), p.end(), m - pairs[i])]);
            ans = max(ans, pairs[i] + pairs[le(pairs.begin(), pairs.end(), m - pairs[i])]);
        }
        cout << ans << endl;
    }
    return 0;
}