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 <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589


#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*) array, (T*) (array + N), val);
}
pii Dir[8] = { //?§????
	{ 0 ,1 },{ -1 ,0 },{ 1 ,0 },{ 0 ,-1 },
	{ 1 ,1 },{ 1 ,-1 },{ -1 ,1 },{ -1 ,-1 }
};

signed main() {
	int n, m;

	while (scanf("%d %d", &n, &m)) {
		if (!n)
			break;
		vector<int> po(n);
		REP(i, n) {
			scnaf("%d", &po[i]);
		}
		po.push_back(0);
		vector<int> toku;
		REP(i, n + 1) {
			REP(j, n + 1) {
				toku.push_back(po[i] + po[j]);
			}
		}
		sort(ALL(toku));
		toku.erase(unique(ALL(toku)),toku.end());
		int ans = 0;
		for (auto itr : toku) {
			auto itr2 = --upper_bound(ALL(toku),m - itr);
			if (itr2 != toku.end())
				ans = max(itr + *itr2, ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

int n, m, point1[1001], sum, ans;
vector<int> point2;

int solve(int start, int end, int border){
    int mid;
    while(1){
        if((end + start) % 2){
            mid = (end + start) / 2 + 1;
        } else{
            mid = (end + start) / 2;
        }

        if(point2[mid] > border){
            end = mid;
        } else{
            start = mid;
        }

        if(end - start == 1){
            if(point2[end] < border){
                return point2[end];
            } else{
                return point2[start];
            }
        }
    }
}

int main(){
    while(cin >> n >> m && n && m){
        for(int i = 0; i < n; i++){
            cin >> point1[i];
        }
        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < n + 1; j++){
                point2.push_back(point1[i] + point1[j]);
            }
        }

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

        for(int i = 0; i < point2.size(); i++){
            if(point2[i] > m){
                continue;
            }
            ans = max(ans, point2[i] + solve(0, point2.size() - 1, m - point2[i]));
        }

        cout << ans << endl;

        memset(point1, 0, sizeof(point1));
        point2.clear();
        ans = 0;
    }

    return 0;
}

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

using namespace std;

int N;
int M;
int P[1000];
int ans=0;

void func(int n,int dart,int sum){
    ans=max(ans,sum);
    if(dart==4 || n<0)return ;
    
    
    if(sum+P[n]<=M){
        func(n-1,dart+1,sum+P[n]);
        func(n,1+dart,sum+P[n]);
        func(n-1,dart,sum);
    }
    else {
        func(--n,dart,sum);
        
    }
    
    return ;
}
int main(){
    while(cin>>N>>M,N,M){
        clock_t start =clock();
        for(int i=0;i<N;i++){
            cin>>P[i];
        }
        sort(P,P+N);
        func(N-1,0,0);
        clock_t end = clock();
        cout<<"time = "<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
        cout<<ans<<endl;
        ans=0;
    }
    
    
}

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

#define MAX_N 1000
int P[MAX_N + 1];
int N, M;
int table[5][MAX_N + 1];

int dfs(int a, int b, int c){
	if(c > M) return P[b] * -1;
	if(a == 0) return P[b];
	if(table[a][b] >= 0) return table[a][b];
	
	int cho = b;
	while(cho <= N){
		int d = dfs(a - 1, cho, c + P[b]) + P[b];
		table[a][b] = max(table[a][b], (d > M)? 0 : d);
		cho++;
	}
	
	return table[a][b];
}

int main(){
	ifstream ifs("input.txt");
	
	for(int i = 0; i < 5; i++){
		for(int j = 0; j <= MAX_N; j++){
			table[i][j] = -1;
		}
	}
	
	ifs >> N >> M;
	
	p[0] = 0;
	for(int i = 1; i <= N; i++) ifs >> P[i];
	
	printf("%d\n", dfs(4, 0, 0));
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long n;
	unsigned long long m;
	while(cin>>n>>m,n!=0||m!=0){
		int k=0;
	unsigned long long  s=0;
		vector<unsigned long long> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0&&k!=4){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
				k++;
			}
			else
				n--;
			
		}
		cout<<s<<endl;
	}
	
}

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

#define MAX_N (1000)
#define MAX_M (2 * 1e8)

using namespace std;

int field[MAX_N + 1];
int fld[(MAX_N + 1) * (MAX_N + 1)];

int N,M;

int ans;

int bin_search(int tar,int *s,int *e){
	int result;
	int width = e - s;
	while(true){
		width = e - s;
		if(width <= 1){
			result = *s;
			break;
		}

		if(*(s + (width / 2)) <= tar){
			s = s + (width / 2);
		}
		else{
			e = s + (width / 2) - 1;
		}
	}
	
	return result;
}

void solve(){
	for(int i = 0;i <= N;i++){
		for(int j = 0;j <= N;j++){
			fld[(N + 1) * i + j] = field[i] + field[j];
		}
	}

	sort(fld,fld + (N + 1) * (N + 1));

	int rest;
	int sample;
	for(int i = 0;i <= N;i++){
		for(int j = 0;j <= N;j++){
			rest = M - (field[i] + field[j]);
			if(rest < 0) break;
			sample = bin_search(rest,fld,fld + (N + 1) * (N + 1));
			ans = max(ans,field[i] + field[j] + sample);
		}
	}

	return ;
}

int main(){

	while(true){
		scanf("%d%d",&N,&M);
		if(N == 0 && M == 0) break;
		for(int i = 1;i <= N;i++){	//fiest element is 0;
			scanf("%d",field + i);
		}

		solve();

		printf("%d\n",ans);

	}

	return 0;
}