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<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 = 1000000; 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 += 100000000;

					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 <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 1005
#define LSIZE 1000005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;
ll M;
ll T[SIZE];
set<ll> S;
ll K;

ll solve()
{
	scanf("%d %lld",&n,&M);
	if(n+M ==0)return -1;
	for(int i=1;i<=n;i++)scanf("%lld",&T[i]);
	T[0]=0;
	S.clear();
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			S.insert(T[i]+T[j]);
		}
	}
	ll maxv=0;
	set<ll>::iterator ii;
	for(ll i=M;i>=0;i--){
		if(i==maxv)return maxv;
		for(set<ll>::iterator it=S.begin();it!=S.end();it++){
			ii = lower_bound(S.begin(),S.end(),(i-(*it)));
			if(*ii == (i-(*it))){
				return i;
			}else{
				maxv = max(maxv,(*it)+(*ii));
			}
		}
	}
	return 0;
}

int main()
{
	while(1){
		ll ans = solve();
		if(ans==-1)break;
		else printf("%lld\n",ans);
	}
	return 0;
}

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

using namespace std;

typedef vector<int> VI;

int binary_search(int tmp_m, VI &second, int i) {
    int left = 0;
    int right = second.size() - 1;
    int mid = (left + right) / 2 + 1;
    while(1) {
        if (right - left <= 1) {
            return second[left] + second[i];
        }
        mid = (left + right) / 2;
        if (second[mid] + second[i] <= tmp_m) {
            left = mid;
        } else {
            right = mid;
        }
    }
    return 0;
}

int main(void) {
    while (1) {
        int n;
        int m;
        cin >> n >> m;
        if (n == 0 && m == 0) {
            break;
        }
        VI table(n + 1);
        VI first;

        table[0] = 0;
        for (int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            if (temp <= m) {
                table.push_back(temp);
            }
        }

        for (int i = 0; i < (int)table.size(); i++) {
            for (int j = i; j < (int)table.size(); j++) {
                int sum = table[i] + table[j];
                if (sum <= m) {
                    first.push_back(table[i] + table[j]);
                }
            }
        }
        sort(first.begin(), first.end());

        int ans = 0;
        for (int i = 0; i < (int)(first.size()); i++) {
            int total = binary_search(m, first, i);
            if (total <= m) {
                ans = total;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = INT_MAX;
const ll LLINF = INT64_MAX;
const double PI = acos(-1);
const int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (true)
    {
        ll n, m;
        cin >> n >> m;
        if (n == 0 && m == 0)
        {
            break;
        }
        vector<ll> p(n + 1, 0);
        for (ll i = 1; i < n + 1; i++)
        {
            cin >> p[i];
        }
        vector<ll> kouho;
        for (ll i = 0; i < n + 1; i++)
        {
            for (ll j = 0; j < n + 1; j++)
            {
                kouho.push_back(p[i] + p[j]);
            }
        }
        sort(ALL(kouho));
        ll sz = kouho.size();
        ll ans = 0;
        for (ll i = 0; i < sz; i++)
        {
            ll v = m - kouho[i];
            if (v < 0)
            {
                continue;
            }
            auto itr = upper_bound(ALL(kouho), v);
            if (itr == kouho.begin())
            {
                continue;
            }
            itr--;
            chmax(ans, kouho[i] + *itr);
        }
        cout << ans << endl;
    }
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int lislis[1003000];
int main(){
    while(1){
        int N,M;
        cin>>N>>M;
        if(N==0&&M==0)break;
        int lis[1002]={0};
        for(int i=1;i<=N;i++)cin>>lis[i];
        for(int i=0;i<1003000;i++)lislis[i]=0;
        for(int i=0;i<=N;i++){
            for(int j=0;j<=N;j++)lislis[i*(N+1)+j]=lis[i]+lis[j];
        }
        sort(lislis,lislis+((N+1)*(N+1)));
        lislis[(N+1)*(N+1)]=lislis[(N+1)*(N+1)-1];
        int maxans=0;
        for(int i=0;i<(N+1)*(N+1);i++){
            int l=0,r=(N+1)*(N+1),mid=(l+r)/2;
            for(int j=0;j<30;j++){
                if(lislis[i]+lislis[mid]<=M)l=mid;
                else r=mid;
                mid=(l+r)/2;
            }
            /*
            printf("[%d] [%d][%d][%d]\n",i,l,mid,r);
            */
            if(lislis[i]+lislis[l]<=M)maxans=max(maxans,lislis[i]+lislis[l]);
        }
        /*
        for(int i=0;i<20;i++)printf("[%d]",lis[i]);
        puts("");
        for(int i=0;i<30;i++)printf("[%d]",lislis[i]);
        puts("");
        */
        cout<<maxans<<endl;
    }
    return 0;
}