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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
typedef pair<ll, P> E;
#define MOD (1000000007ll)
#define l_ength size
#define PI 3.14159265358979

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	b += MOD;
	a += b;
	a %= MOD;
}

int n,s;
ll m,p[1234567];

ll search(ll x){
	int l,r,mid;
	if(x>m){
		return 0ll;
	}
	l = 0; r = s;
	while((r-l)>1){
		mid = (l+r)/2;
		if(x+p[mid]>m){
			r = mid;
		}else{
			l = mid;
		}
	}
	return ((x+p[r]>m)?(x+p[l]):(x+p[r]));
}

int main(void){
	int i,j;
	ll ans;
	cin >> n >> m;
	while(!(n==0&&m==0ll)){
		ans = 0ll; s = n*n+n+1;
		for(i=0; i<n; ++i){
			cin >> p[i];
		}
		for(i=0; i<n; ++i){
			for(j=0; j<n; ++j){
				p[n+i*n+j] = p[i]+p[j];
			}
		}
		p[s-1] = 0ll;
		sort(p,p+s);
		for(i=0; i<s; ++i){
			ans = max(ans,search(p[i]));
		}
		cout << ans << endl;
		cin >> n >> m;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, p[1010];
int dp[1010];

int main(){
	while (cin >> n >> m&&n + m){
		/*fill(dp, dp + 1010, 0);
		p[0] = 0;
		FOR(i, 1, n + 1)cin >> p[n];
		REP(i, 4){
			REP(j, n + 1){
				int now = dp[j];
				REP(k, n + 1){
					if (dp[j] + p[k] <= m){
						
					}
				}
			}
		}*/
		REP(i, m){}
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
   
int n, m;
vector<int> p1, p2;
int main()
{
  while(true){
    scanf("%d%d", &n, &m);
    if(!n && !m)
      break;
  
    p1.clear();
    p2.clear();
       
    //chap1
    p1.push_back(0);
    for(int i = 0; i < n; i++){
      int p;
      scanf("%d", &p);
      p1.push_back(p);
    }
   
    //chap2
    for(int i = 0; i <= n; i++)
      for(int j = i; j <= n; j++)
    p2.push_back(p1[i] + p1[j]);
   
    sort(p2.begin(), p2.begin() + p2.size());
   
    //chap3
    int ans = 0;
    for(int i = 0; i < p2.size(); i++){
      vector<int>::iterator count2 = upper_bound(p2.begin(), p2.end(), m - p2[i]);
      if(count2 != p2.begin())
    ans = max(ans, *(count2 - 1) + p2[i]);
    }
    printf("%d", ans);
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;

int solve(vector<int> p, int n, int m) {
    vector<int> cd(n * n);
    for (int l = 0; l < n; ++l) {
        for (int i = 0; i < n; ++i) {
            cd[n * l + i] = p[l] + p[i];
        }
    }

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

    int mx = -1;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int r = m - (p[i] + p[j]);
            auto it = upper_bound(cd.begin(), cd.end(), r);
            if(*(--it) + p[i] + p[j] <= m )  {
                mx = max(mx, *it + p[i] + p[j]);
            }
        }
    }
    return mx;
}
int main() {
    while(true) {
        int n,m;
        cin >> n >> m;
        if(!n && !m) break;

        vector<int> p(n + 1);
        for (int i = 0; i < n; ++i) {
            cin >> p[i];
        }
        p[n] = 0;

        int res = solve(p, n + 1, m);
        cout << res << endl;
    }

    return 0;
}

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

set<long long> sep;
long long ary[1010];
int main(){
	int N;
	long long M,hog,res;
	set<long long>::iterator it,it2;
	while(1){
		sep.clear();
		cin>>N>>M;
		if(N==0&&M==0) break;
		for(int i=0;i<N;i++){
			cin>>ary[i];
		}
		ary[N]=0;
		for(int i=0;i<=N;i++){
			for(int j=i;j<=N;j++){
				if(ary[i]+ary[j]<=M){
					sep.insert(ary[i]+ary[j]);
				}
			}
		}
		res=0;
		it2=sep.end();
		it2--;
		for(it=sep.begin();it!=sep.end();it++){
			while((*it)+(*it2)>M)it2--;
			res=max(res,(*it)+(*it2));
		}
		cout<<res<<endl;

	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
typedef long long L;
using namespace  std;
int main(){
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
	int n;
	L m,p[1001],sum=0;
	vector<L> sum1;
	vector<L>::iterator it,ele;
	scanf("%d %lld",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%lld",&p[i]);
	}
	p[n]=0;
	for(int i=0;i<=n;i++){
		for(int i2=0;i2<=n;i2++){
			sum1.push_back(p[i]+p[i2]);
		}
	}
	sort(sum1.begin(),sum1.end());
	for(it=sum1.begin();it<sum1.end();it++){
		if(*it>m)
			break;
		ele=upper_bound(sum1.begin(),sum1.end(),(m-(*it) ) );
		if(ele!=sum1.begin()){
			ele--;
		}
		if(sum<=(*it)+(*ele)){
			sum=(*it)+(*ele);
		}
		if(sum==m)
			break;
	}
	printf("%lld\n",sum);
	return 0;
}