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

using namespace std;

int m,n;
int a[1002];
int aa[1002001];

int ans,Max,size;

int main(){
	
	while (1) {
		scanf("%d%d",&n,&m);
		ans=0;
		if (n==0&&m==0) {
			break;
		}
		for (int i=0; i<n; i++) {
			scanf("%d",&a[i]);
		}
		a[n++]=0;
		
		size=0;
		for(int c=0;c<n;c++){
			for (int d=c; d<n; d++) {
				aa[size++]=a[c]+a[d];
			}
		}
		sort(aa,aa+size);
		
		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) {
				if (m-a[i]-a[j]>0) {
					int l=0,r=size-1;
					Max=0;
					while (l<=r) {
						if (aa[size-1]+a[i]+a[j]<ans) {
							break;
						}
						int k=(l+r)/2;
						if (aa[k]<(m-a[i]-a[j])&&aa[k]>Max) {
							Max=aa[k];
						}
						if(aa[k]<(m-a[i]-a[j]))l=k+1;
						else r=k-1;
					}
					if (Max+a[i]+a[j]>ans) {
						ans=Max+a[i]+a[j];
					}
				}
			}
		}
		printf("%d\n",ans);
	}
}

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

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

int main() {
	
	while(cin >> N >> M, N + M > 0) {
	
		int N, M;
		int P[1111];
		vector<int> PP;
		
		for(int i= 0; i < N; i++) {
			cin >> P[i];
		}
		
		P[N++] = 0;
		
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < N; j++) {
				PP.push_back(P[i] + P[j]);
			}
		}
		
		sort(PP.begin(), PP.end());
		
		int ans = 0;;
		
		for(int i = 0, m; i < PP.size(); i++) {
			m = M - PP[i];
			if(m < 0) continue;
			int it = *(upper_bound(PP.begin(), PP.end(), m) - 1);
			ans = max(ans, PP[i] + it );
			//cout << PP[i] << " " << it << endl;
		}
		
		cout << ans << endl;
	}

	return 0;

}

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

using namespace std;
typedef long long ll;

vector<int> memo;//x+x
int ns[1000];
int N,M;

int score(int x){
	if(x > M){
		return 0;
	}else{
		return x;
	}
}

int solve(){
	int ans = 0;
	for(const auto x : memo){
		ans = max(ans,score(x));
		if(x < M){
			auto it = upper_bound(memo.begin(),memo.end(),M-x);
			if(it!=memo.begin()){
				--it;
				ans = max(ans,score(x+ *it));
			}
		}
	}
	return ans;
}

int main(){
	while(true){
		memo.clear();
		cin >> N >> M;
		if(N == 0 && M==0)break;
		for(int i=0;i<N;++i){
			int t;
			scanf("%d",&t);
			ns[i] = t;
		}
		memo.push_back(0);
		for(int i=0;i<N;++i){
			memo.push_back(ns[i]);
			for(int j=i;j<N;++j){
				memo.push_back(ns[i]+ns[j]);
			}
		}
		sort(memo.begin(),memo.end());
		memo.erase(unique(memo.begin(),memo.end()),memo.end());
		cout << solve() << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

int binary_search(int tmp_m, VI second) {
    if (tmp_m <= 0) {
        return 0;
    }
    int left = 0;
    int right = second.size() - 1;
    int mid = (left + right) / 2 + 1;
    while(1) {
        if (right - left <= 1) {
            //cout << "LEFT = " << left << " " << second[left] << endl;
            //cout << "RIGHT = " << right << " " << second[right] << endl;
            return second[left];
        }
        mid = (left + right) / 2;
        if (second[mid] <= 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++) {
            cin >> table[i + 1];
        }

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

        int first_end = first[first.size() - 1];
        int ans = 0;
        for (int i = 0; i < (int)(first.size()); i++) {
            //cout << "I = " << i << endl;
            //cout << "ACT = " << m - first[i] << endl;
            int total;
            if (m - first[i] <= first_end) {
                total = first[i] + binary_search(m - first[i], first);
            } else {
                total = first[i];
            }
            if (total <= m) {
                if (ans < total) {
                    ans = total;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
//http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
#include<cstring>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define P pair<int,int>
#define ll __int64

int n,m;
int t[10000];
const int MA=1000000;
int s1[MA],s2[MA];

int ya[4];

int main(){

while(cin>>n>>m,n||m){
rep(i,MA)s1[i]=s2[i]=0;
rep(i,10000)t[i]=0;

for(int i=1;i<=n;i++)cin>>t[i];
int g=0;

for(int i=0;i<=n;i++){
	for(int j=0;j<=n;j++){
		s1[i*(n+1)+j]=t[i]+t[j];
		g++;
	}
}


sort(s1,s1+g);
rep(i,g)s2[i]=s1[i];

int ans=0;

for(int i=0;i<g;i++){
	int now=s1[i];

	int l=0;
	int r=g-1;
	int mid=(l+r)/2;
	int x=s2[mid];

	for(int i=0;i<70;i++){
		if(x+now==m)break;
		if(x+now>m){
			r=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
		else{
			l=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
	}

	//cout<<" "<<x<<" "<<now<<" "<<x+now<<endl;
	if(x+now<=m)
	ans=max(ans,x+now);
}
cout<<ans<<endl;
}
	return 0;
}