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<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;
}

C++: #include <iostream>
using namespace std;
 
int main() {
	for(;;){
		/*入力*/
		int N,M,count=0;
		cin >> N >> M;
		if(N==0 && M==0)exit(0);
		int P[N+1],array[M+1]={0};
		P[0]=0;
		for(int i=1;i<=N;i++)cin >> P[i];
		for(int i=0;i<=N;i++)array[P[i]]=1;
 
		/*２投(０投１投を含む)で1からMまでが可能か判定*/
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				if(P[i]+P[j] <= M)array[P[i]+P[j]]=1;
			}
		}
 
		/*４投に拡張*/
		for(int i=0;i<=M;i++){
			if(array[i]==1)count++;
		}
		int k=0,p[count];
		for(int i=0;i<=M;i++){
			if(array[i]==1){
				p[k]=i;
				k++;
			}
		}
		for(int i=0;i<count;i++){
			for(int j=0;j<count;j++){
				if(p[i]+p[j] <= M)array[p[i]+p[j]]=1;
			}
		}
 
		/*判定出力*/
		for(int i=M;i>0;i--){
			if(array[i]==1){
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}

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

int main()
{
	int n, m;
	int point[1000 + 10] = {0};
	vector<int> sec;

	int sum;

	int i, j;

	while (cin >> n >> m, n != 0 && m != 0)
	{
		sec.clear();
		sum = 0;
		for (i = 1; i <= n; i++)
		{
			cin >> point[i];
		}

		for (i = 0; i <= n; i++)
		{
			for (j = i; j <= n; j++)
			{
				sec.push_back(point[i]+point[j]);
			}
		}

		sort(sec.begin(), sec.end());

		for (i = sec.size() - 1; i >= 0; i--)
		{
			for (j = i; j >= 0; j--)
			{
				if (sec[i] + sec[j] <= m)
				{
					sum = max(sum, sec[i] + sec[j]);
					break;
				}
			}
		}

		cout << sum << endl;
	}

	return 0;
}

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


#define rep(X, Y) for(int (X) = 0; (X) < (Y); ++(X))
#define MAX 1001

typedef long long ll;

int n;
ll m, b[MAX], h[MAX*MAX], res = -1;

int main(){
	while(scanf("%d%lld", &n, &m), n|m){

		rep(i, n){
			scanf("%lld", b + i + 1);
		}
		++n;

		rep(i, n){
			rep(l, n){
				h[i * n + l] = b[i] + b[l];
			}
		}

		std::sort(h, h + n * n);

		res = -1;
		rep(i, n*n){
			res = std::max(res, *(std::upper_bound(h, h + n * n, m - h[i]) - 1) + h[i]);
		}

		printf("%lld\n", res);
	}

	return 0;
}

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

int main()
{
	int n,m,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&P[i]);
		}
		for(int i = 0; i < n; i++){
			for(int j = i; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			if(P[i] > m){
				break;
			}
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			MAX = max(MAX,P[i] + *it);
		}
	
		printf("%d\n",MAX);
	}
	
	return 0;
}