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 <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int main() {
    int N, M;
    while(cin >> N >> M, N | M) {
        vector<ll> P(N);
        rep(i, N) { cin >> P[i]; }
        P.pb(0);
        vector<ll> P2;
        rep(i, N + 1) {
            FOR(j, i, N + 1) { P2.pb(P[i] + P[j]); }
        }
        sort(All(P2));
        ll res = 0;
        for(int i = 0; i < P2.size(); i++) {
            if(P2[i] > M)
                continue;
            auto it = lower_bound(All(P2), M - P2[i]);
            if(it != P2.begin())
                it--;
            res = max(res, P2[i] + *it);
        }
        cout << res << endl;
    }
    return 0;
}

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

int main()
{
	int n,m,in,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&in);
			P.push_back(in);
		}
		for(int i = 0; i < n; i++){
			for(int j = i; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			if(P[i] > m){
				break;
			}
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			MAX = max(MAX,P[i] + *it);
		}
	
		printf("%d\n",MAX);
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main(){
	while(1){
		int n,m,ans=0,MAX=0;
		cin>>n>>m;
		vi d(n+1),w;
		if(n==0 && m==0) break;
		d[0]=0;
		rep(i,1,n+1) cin>>d[i];
		rep(i,0,n+1){
			rep(j,0,n+1){
				w.pb(d[i]+d[j]);
			}
		}
		sort(all(w));
		rep(i,0,w.size()){
			//vector<int>::iteratar it=upper_bound(all(w),m-w[i]);
			int p=upper_bound(all(w),m-w[i])-w.begin();
			if(p==0) continue;
			MAX=max(MAX,w[i]+w[p-1]);
		}
		o(MAX);
	}
}

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

int A[2000];
int KK[1000];

int N,M;

int k;
void getK()
{
	k = 0;
	for(int i =0; i<=N; i++)
		for(int j = i; j<=N; j++)
			KK[k++] = A[i]+A[j];	
	
	sort(KK,KK+k);		

}


int binarySearch(int a)
{
	int left = 0;
	int right = k;
	int mid;
	while(left+1<right)
	{
		mid = (left+right)/2;
		if(KK[mid] <= a) left = mid;
		else right = mid;
	}

	return KK[left];
}


void solve()
{
	
	int maxS = 0;
	for(int i =0; i<=N;i++)
		for(int j = 0; j <=N; j++)
		{
			int temp = binarySearch(M-A[i]-A[j])+A[i]+A[j];	
			if(temp > maxS && temp <= M)
				maxS = temp;
		}
	cout << maxS << endl;
}

int main()
{
	A[0] = 0;
	while(cin >>N>>M && N||M)
	{
		
		for(int i = 1; i<=N;i++)
			cin >> A[i];
		
		sort(A,A+N+1);
		getK();
		
		solve();
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

int main()
{
    int N, M;
    while (cin >> N >> M, N) {
        vector<int> a(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                a.push_back(a[i] + a[j]);
            }
        }

        sort(a.begin(), a.end());
        
        N = a.size();
        
        int res = 0;
        for (int i = 0; i < N; i++) {
            int m = M - a[i];
            if (m < 0) break;
            
            int l = 0, r = N;
            while (r - l > 1) {
                int mid = (l + r) / 2;
                if (a[mid] < m) {
                    l = mid;
                    res = max(res, a[i] + a[l]);       
                } else {
                    r = mid;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

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

using namespace std;

int main(void)
{
	int n, m;

	while (cin >> n >> m, n || m){
		vector <int> p(n + 1);
		for (int i = 0; i < n; i++){
			scanf("%d", &p[i]);
		}
		p[n] = 0;
		n++;
		sort(p.begin(), p.end());

		vector <int> sum(n*n);
		int cnt = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				sum[cnt] = p[i] + p[j];
				// cout << sum[cnt] << " " << p[i] << " " << p[j] << endl;
				cnt++;
			}
		}

		int ret = 0;
		for (int i = 0; i < n*n; i++){
			int diff = m - sum[i];
			if (diff < 0) continue;
			int up = *(upper_bound(sum.begin(), sum.end(), diff)-1);
			ret = max(ret, sum[i] + up);
		}

		cout << ret << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,m,k;
  while(cin>>n>>m){
    if(n==0&&m==0) break;
    int t[m+1];
    for(int i=0;i<=m;i++) t[i] = 0;
    for(int i=0;i<n;i++){
      cin >> k;
      for(int j=0;j<4;j++){
	for(int g=m-1;g>=0;g--){
	  if(t[g]>=1&&g+k<=m&&t[g]<4){
	    if(t[g+k]==0||t[g+k]>t[g]+1) t[g+k] = t[g] + 1;
	  }
	  if(g==0) t[k] = 1;
	}
      }
    }
    for(int i=m;i>=0;i--){
      if(t[i]!=0){
	cout << i << endl;
	break;
      }
    }
  }
  return 0;
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;
   
int32_t N, M, P[1123];
std::vector<int32_t>list;
int32_t ans;
   
int main()
{
    while (true)
    {
        using std::begin;using std::end;

        N = M = 0;
        memset(P, 0, sizeof(P));
        list.clear();
        ans = 0;
   
        in >> N >> M;
        if (N == 0 && M == 0) { break; }
        rep(i, N) { in >> P[i]; }
   
        P[N] = 0;
        N++;
        //std::sort(P, P + N);
        rep(i, N)
        {
            rep(j, N) { list.push_back(P[i] + P[j]); }
        }
        std::sort(begin(list), end(list));
        for (auto& i : list)
        {
            if (i > M) { break; }
            auto iter = upper_bound(begin(list), end(list), M - i);
            --iter;
            ans = std::max(ans, i + *iter);
        }
   
        out << ans << std::endl;
    }
    return 0;
}