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 <algorithm>
#include <vector>

#define ef(e, a, b) ((e) ? (a) : (b))

using namespace std;

int main ( void )
{
	int n, m;
	
	vector<int> x;

	while ( cin >> n >> m, (n | m) )
	{
		vector<int> v(n);
		int ans = 0;
		
		for (int i = 0; i < n; ++i)
		{
			cin >> v[i];
		}
		
		for (int i = 0; i < n; ++i)
			for (int j = i; j < n; ++j)
			{
				v.push_back(v[i] + v[j]);
			}
			
		sort(v.begin(), v.end());
		
		for (int i = 0, sz = v.size(); i < sz; ++i)
		{
			if (v[i] > m) break;
			
			int t = m - v[i];
			
			vector<int>::iterator it = lower_bound(v.begin(), v.end(), t);
			if (t >= *it)
			{
				ans = max(ans, v[i] + *it);
			}
			else
			{
				--it;
				ans = max(ans, v[i] + *it);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int main(void)
{
   vector<int>a;
   priority_queue<int>ans;
   a.push_back(0);

   int n,m;
   int input;
   scanf("%d%d",&n,&m);
   for(int i = 0;i < n;i++)
   {
      scanf("%d",&input);
      a.push_back(input);
   }

   int s = a.size();
   int one,two;
   for(int i = 0;i < s;i++)
      for(int j = 0;j < s;j++)
      {
         one = a[i],two = a[j];
         if(one + two <= m) a.push_back(one + two);
      }

   s = a.size();
   for(int i = 0;i < s;i++)
      for(int j = 0;j < s;j++)
      {
         one = a[i],two = a[j];
         if(one+two <= m) ans.push(one+two);
      }
   cout<<ans.top()<<endl;

   return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

int N, M, arr[1000];

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    while (true) {
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        arr[0] = 0; ++N;
        for (int j = 1; j < N; ++j) {
            cin >> arr[j];
        }
        vector<int> arr2;
        for (int j = 0; j < N; ++j) for (int k = 0; k < N; ++k) if (arr[j] + arr[k] <= M) {
            arr2.push_back(arr[j] + arr[k]);
        }
        sort(begin(arr2), end(arr2));
        const int NN = arr2.size();

        int ans = 0, r = NN-1;
        for (int j = 0; j < NN; ++j) {
            while (arr2[j] + arr2[r] > M) --r;
            ans = max(ans, arr2[j] + arr2[r]);
        }

        cout << ans << endl;
    }
    return 0;
}

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

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int N, M;
int P[1100];
signed main() {
    while (true) {
        cin >> N >> M;
        if (N == 0) break;
        for (int i = 0; i < N; i++) cin >> P[i];
        P[N] = 0;
        vint half;
        for (int i = 0; i < N + 1; i++) {
            for (int j = 0; j < N + 1; j++) {
                half.push_back(P[i] + P[j]);
            }
        }
        sort(half.begin(), half.end());
        int ans = 0;
        for (int i = 0; i < half.size(); i++) {
            auto it = upper_bound(half.begin(), half.end(), M - half[i]);
            if (it != half.begin()) {
                int cur = half[i] + *(it - 1);
                if (M - cur < M - ans) ans = cur;
            }
        }
        cout << ans << endl;
    }
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <algorithm>
#include <array>
#include <numeric>
using namespace std;

const auto gcu = getchar_unlocked;
const auto pcu = putchar_unlocked;
#define _NE noexcept
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _sc _il bool scan
_T T in(int c)_NE{T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_il int in()_NE{return in<int>(gcu());}
_T T scan(T &n)_NE{int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c)_NE{c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s)_NE{int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t)_NE{return scan(h)&&scan(t...);}
#define _ot _il void out
#define _ol _il void outl
_ot(bool b)_NE{pcu('0'+b);}
_ot(const char *s)_NE{while(*s)pcu(*s++);}
_ot(char c)_NE{pcu(c);}
#ifdef _GLIBCXX_STRING
_ot(string s)_NE{for(char c:s)pcu(c);}
#endif
_T _ot(T n)_NE{static char b[20];char *p=b,m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=n%10*m+'0',n/=10;while(p!=b)pcu(*--p);}
_ol()_NE{out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _ot(vector<T> &v)_NE{for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _ot(H&& h, T&&... t)_NE{out(h);out(move(t)...);}
template <typename... T> _ol(T&&... t)_NE{out(move(t)...);outl();}

#define all(o) o.begin(), o.end()

void radix_sort(vector<int> &v) {
	vector<int> t(v.size());
	array<int, 256> c, s;
	for (int i = 0; i < 32; i += 8) {
		fill(all(c), 0);
		for_each(all(v), [&](int j) {c[(j >> i) & 255]++;});
		s.front() = 0;
		partial_sum(all(c) - 1, s.begin() + 1);
		for_each(all(v), [&](int j) {t[s[(j >> i) & 255]++] = j;});
		swap(v, t);
	}
}

int main() {
	for (int N, M; N = in(), M = in();) {
		vector<int> t(N), u;
		for (int &p: t)
			p = in();
		t.push_back(0);
		for (auto i = t.begin(); i != t.end(); i++)
			for (auto j = i; j != t.end(); j++)
				if (*i + *j <= M)
					u.push_back(*i + *j);
		radix_sort(u);
		int r = 0;
		for (auto i = u.begin(), e = u.end() - 1; i <= e; i++) {
			for (; *i + *e >= M; e--)
				;
			r = max(r, *i + *e);
		}
		outl(r);
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
    while(true){
        int n,m;
        cin >> n >> m;
        if(n==0)break;
        vector<int> list(n);
        for(int i=0;i<n;i++){
            cin >> list[i];
        }
        sort(list.begin(),list.end());
        vector<vector<int> > map(n,vector<int>(5,0));
        for(int i=1;i<=4;i++){
           if(list[0]*i<=m)map[0][i]=list[0]*i;
           else map[0][i]=map[0][i-1];
        }
        for(int i=1;i<n;i++){
            if(list[i]<=m)map[i][1]=max(list[i],list[i-1]);
            else list[i-1];
        }
        for(int i=1;i<n;i++){
            for(int j=2;j<=4;j++){
                map[i][j]=map[i][j-1];
                for(int k=0;k<=i;k++){
                for(int l=0;l<=i;l++){
                    if(map[k][j-1]+list[l]<=m){
                        map[i][j]=max(map[k][j-1]+list[l],map[i][j]);
                    }
                }
                }
            }
        }
        cout << map[n-1][4] << endl;
    }
    return 0;
}