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;
static const int MAX_N = 1000;
int main(){
	int N, M;
	int point[MAX_N + 1];
	int a[(MAX_N + 1) * (MAX_N + 1)];
	for(;;){
		scanf("%d %d", &N, &M);
		if(N == 0 && M == 0) break;
		point[0] = 0;
		for(int i = 1; i <= N; i++) scanf("%d", &point[i]);
		int index = 0;
		for(int i = 0; i < N + 1; i++){
			for(int j = 0; j < N + 1; j++){
				a[index] = point[i] + point[j];
				index++;
			}
		}
		sort(a, a + (N + 1) * (N + 1));
		int num_a = (N + 1) * (N + 1);
		int ans = 0;
		for(int i = 0; i < num_a; i++){
			if(a[i] > M) break;
			int lb = 0, ub = num_a - 1;
			int mid;
			while(ub - lb > 1){
				mid = (lb + ub) / 2;
				if(a[i] + a[mid] > M) ub = mid;
				else lb = mid;
			}
			mid = (lb + ub) / 2;
			if(a[i] + a[mid] > ans){
				ans = a[i] + a[mid];
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

using namespace std;

typedef vector<int> vi;

#define PB push_back
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define all(a) a.begin(),a.end()

int P[1001];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int N, M;
	while (cin >> N >> M, N || M) {
		rep1(i, N) cin >> P[i];

		vi A;

		rep(i, N + 1) rep(j, i + 1) if (P[i] + P[j] <= M) A.PB(P[i] + P[j]);

		sort(all(A));

		int ma = 0;

		int l = 0, r = A.size() - 1;

		while (l <= r) {
			while (A[l] + A[r] > M) r--;
			ma = max(ma, A[l] + A[r]);
			l++;
		}

		cout << ma << endl;
	}
}

C++: #include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
	int a,b,mato[1000];
	vector<int> nums;
	while(scanf("%d%d",&a,&b),a,b){
		int res=0;
		nums.clear();
		for(int i=0;i<a;i++)scanf("%d",mato+i);
		for(int i=0;i<=a;i++)
			for(int j=0;j<=a;j++)
				nums.push_back(mato[i]*!!(i-a)+mato[j]!!(j-a));
				
		sort(nums.begin(),nums.end());
		vector<int>::iterator it=nums.begin();
		while(it!=nums.end() && b>=*it){
			vector<int>::iterator itt=upper_bound(nums.begin(),nums.end(),b-*it);
			res=max(res,*it+++*--itt);
		}
		printf("%d\n",res);
	}
	return 0;
}

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

int main() {

	int N, M;
	int P[1111];
	vector<int> PP;
	
	cin >> N >> M;
	
	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<set>
#include<vector>
using namespace std;
typedef unsigned int uint;
uint maxv;
void solve(uint now,uint m,uint *a,int n){
  int r=n-1,l=0,mid;
  while(l <=r){
    mid = (l+r)/2;
    if ( a[mid]+now>m)r=mid-1;
    else if ( a[mid]+now <m)l=mid+1;
    else break;

  }
  if ( now+a[mid]>maxv&& now+a[mid]<m)maxv=now+a[mid];
}


main(){
  int n;
  uint m;
  while(scanf("%d%d",&n,&m) && n){
    set<uint> S;    
    uint one[n];
    uint two[n*n];
    int p=0;
    for(int i=0;i<n;i++)scanf("%d",&one[i]);
    for(int i=0;i<n;i++){
      for(int j=i;j<n;j++){
	if ( S.find(i+j) == S.end())S.insert(i+j),two[p++]=one[i]+one[j];
      }
    }
    sort(one,one+n);
    sort(two,two+p);
    maxv=0;
    for(int i=0;i<p;i++){
      if ( two[i]+one[0]>m)break;
      if ( two[i]+two[0]<=m)solve(two[i],m,two,p);
      if ( two[i]+one[0]<=m)solve(two[i],m,one,n);
    }
    printf("%d\n",maxv);
  }
  return false;
}

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]);
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int main()
{
	int N, M, D;

	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

		if (N == 0 && M == 0) { break; }

		vector<int> C; C.push_back(0);

		for (int i = 0; i < N; i++)
		{
			scanf("%d", &D);

			if (D <= M)
			{
				C.push_back(D);
			}
		}

		vector<int> r;

		for (int i = 0; i < C.size(); i++)
		{
			for (int j = 0; j < C.size(); j++)
			{
				if (C[i] + C[j] <= M)
				{
					r.push_back(C[i] + C[j]);
				}
			}
		}

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

		int ret = 0;

		for (int i = 0; i < r.size(); i++)
		{
			int ptr = upper_bound(r.begin(), r.end(), M - r[i]) - r.begin() - 1;
			
			if (ptr >= 0)
			{
				ret = max(ret, r[i] + r[ptr]);
			}
		}

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

	return 0;
}

C++: /**
 * File    : JOI_2007_darts.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-3-8 20:45:20
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
int P[1010];
set<int> S;

int main()
{
  while (cin >> N >> M && N && M)
  {
    S.clear();
    for (auto i = 0; i < N; i++)
    {
      cin >> P[i];
    }
    P[N] = 0;
    N++;
    for (auto i = 0; i < N; i++)
    {
      for (auto j = i; j < N; j++)
      {
        S.insert(P[i] + P[j]);
      }
    }
    S.insert(M + 1);
    int ans = 0;
    for (auto i = 0; i < N; i++)
    {
      for (auto j = i; j < N; j++)
      {
        int sum = P[i] + P[j];
        if (sum <= M)
        {
          auto t = S.upper_bound(M - sum);
          t--;
          ans = max(ans, sum + *t);
        }
      }
    }
    cout << ans << endl;
  }
}