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 <iostream>
using namespace std;

int main() {
	for(;;){
		/*入力*/
		int N,M;
		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;
			}
		}
		
		/*４投に拡張*/
		int p[M+1]={0};
		for(int i=1;i<=M;i++){
				if(array[i]==1)p[i]=i;
		}
		for(int i=0;i<=M;i++){
			for(int j=0;j<=M;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 "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int kk[1100001];
int n, m;

int binary_search(int x) {
    if (x <= 0) return 0;
    int l = 0, r = n * n;
    //dump(r - l);
    int i = 0;
    while(r - l >= 1) {
        i = (l + r) / 2;
        if (kk[i] == x) return i;
        else if (kk[i] < x) l = i + 1;
        else r = i;
    }
    while(kk[i] > x) i--;
    return i;
}

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true){
        cin >> n >> m;
        if(n == 0 && m == 0) return 0;
        int k[n+1];
        k[0] = 0;
        rep(i, n) cin >> k[i+1];

        rep(c, n+1) {
            rep(d, n+1) {
                kk[c * n + d] = k[c] + k[d];
            }
        }

        sort(kk, kk + (n+1) * (n+1));
        int ans = 0;
        rep(a, n+1) {
            rep(b, n+1) {
                int index = binary_search(m - k[a] - k[b]);
                int tmp = kk[index] + k[a] + k[b];
                if(tmp <= m) ans = max(ans, tmp);
                tmp = kk[index+1] + k[a] + k[b];
                if(tmp <= m) ans = max(ans, tmp);

            }
        }
        cout << ans << endl;
    }

    return 0;
}

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

int n,m;
int target[1000];

int chek(int cnt, int point,int now)
{
	int i;
	int sum=0;

	if (cnt == 5)
	{
		return point;
	}

	for (i = now; i <= n; i++)
	{
		if (point + target[i] <= m)
		{
			sum = max(sum, chek(cnt + 1, point + target[i],i));

			/*
			num = chek(cnt + 1, point + target[i]);
			if (num <= m)
			{
				max = (max < num) ? max : num;
			}
			*/
		}
	}

	return sum;
}

int main()
{
	int sum;
	int i;

	while (scanf("%d %d",&n,&m), n != 0 || m != 0)
	{
		//printf("AA%d\n",m);
		for (i = 1; i <= n; i++)
		{
			scanf("%d", &target[i]);
		}

		sum = chek(1, 0,0);

		printf("%d\n", sum);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
	long n,maxsc;
	long m,p[100000000];
	while(1){
		cin >> n >> m;
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++){
			cin >> p[i];	
		}
		sort(p,p+n,greater<int>());
		maxsc = 0;
		int cn = 0;
		for(int i=0;i<n||cn<=4;i++){
			for(int j=cn;j<=4;j++){
				if(maxsc+p[i] <= m){
					maxsc = max(maxsc+p[i],maxsc);
					cn++;
				}
			}
			if(maxsc+p[i+7] >= m) break;
		}
		cout << maxsc << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int n, m;
int main()
{
	while (cin >> n >> m, n)
	{
		vector<int> v, w;
		REP(i, n)
		{
			int a;
			cin >> a;
			w.push_back(a);
		}
		w.push_back(0);
		set<int> used;
		REP(i, w.size())REP(j, w.size())
		{
			int t = w[i] + w[j];
			if (used.find(t) == end(used))
			{
				used.insert(t);
				v.push_back(w[i] + w[j]);
			}
		}
		sort(ALL(v));
		int ans = 0;
		REP(i, v.size())
		{
			int tmp = m - v[i];
			if (tmp < 0) continue;
			int k = *(upper_bound(ALL(v), tmp) - 1);
			ans = max(ans, v[i] + k);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;

int p[1001];
int h[1000*1000+1];

int main(){
	int n,m,ans;
	while(cin>>n>>m){
		if(n==0&&m==0) break;
		p[0]=0;
		ans=0;
		for(int i=1; i<=n; ++i){
			cin >> p[i];
		}
		for(int i=0; i<=n; ++i){	
			for(int j=0; j<=n; ++j){
				h[i*n+j]=p[i]+p[j];
			}
		}
		sort(h,h+n*n+1);
		/*
		for(int i=0; i<=n*n; ++i){
			cout << h[i] << "\n";
		}
		*/
		for(int i=0; i<=n; ++i){	
			for(int j=0; j<=n; ++j){	
				int tmp=p[i]+p[j];
				int l=0,r=n*n;
				int cnt=0;
				while(1){
					int mid=(r+l)/2;
					if(cnt>=50){
						if(h[mid]+tmp<=m) ans=max(ans,h[mid]+tmp);
						break;
					}
					if(h[mid]+tmp>m){
						r=mid;
					} else if(h[mid]+tmp<m){
						l=mid;
					} else if(h[mid]+tmp==m){
						ans=m;
						break;
					}
					++cnt;
				}
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m;
int p[1001];
int pp[1500000];
int main() {
    while (scanf("%d%d",&n,&m)) {
        if (!n&&!m) break;
        memset(p, 0, sizeof(p));
        for (int i=0; i<n; i++) scanf("%d",&p[i]);
        sort(p, p+(++n));
        for (int c=0; c<=n; c++) {
            for (int d=0; d<=n; d++) {
                pp[c*n+d]=p[c]+p[d];
            }
        }
        sort(pp, pp+n*n);
        int res=0;
        for (int i=0; i<n*n; i++) {
            int temp = upper_bound(pp,pp+n*n,m-pp[i])-pp-1;
            if (pp[temp]+pp[i]<=m) res = max(res, pp[temp]+pp[i]);
        }
        printf("%d\n",res);
    }
}