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) {
			if (m >= itr) {
				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>
using namespace std;
int main()
{
  while(true){
    int n,m;
    cin>>n>>m;
    if(!n&&!m) break;
    int area[1001],max=0;
    area[0]=0;
    for(int i=1;i<=n;i++){
      cin>>area[i];
    }
    
    for(int i1=0;i1<=n;i1++){
      for(int i2=i1;i2<=n;i2++){
        for(int i3=i2;i2<=n;i3++){
          for(int i4=i3;i4<=n;i4++){
            int sum=area[i1]+area[i2]+area[i3]+area[i4];
            if(sum<=m&&max<sum){
              max=sum;
            }
          }
        }
      }
    }
    cout<<max<<endl;
  }
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];

signed 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;

		A.PB(0);

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

		sort(all(A));

		int ma = 0;

		rep(i, A.size()) {
			int tmp;

			int pos = upper_bound(all(A), M - A[i]) - A.begin();

			if (pos == A.size()) {
				tmp = A.back();
			} else if (pos - 1 == -1) {
				tmp = 0;
			} else {
				tmp = A[pos - 1];
			}

			if (A[i] + tmp <= M) {
				ma = max(ma, A[i] + tmp);
			}
		}

		cout << ma << endl;
	}
}

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];
        }
        int arr2[1002*1002];
        for (int j = 0; j < N; ++j) for (int k = 0; k < N; ++k) {
            arr2[N*j+k] = arr[j] + arr[k];
        }
        const int NN = N*N;
        sort(arr2, arr2+NN);
        
        int ans = 0, r = NN-1;
        for (int j = 0; j < NN && r >= 0; ++j) {
            while (r > 0 && arr2[j] + arr2[r] > M) --r;
            if (arr2[j] + arr2[r] <= M) ans = max(ans, arr2[j] + arr2[r]);
        }

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

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

using namespace std;

int main()
{
	for(int n,m;cin>>n>>m,n|m;){
		vector<int> p(n);
		for(int i=0;i<n;i++)
			cin>>p[i];
		vector<int> a;
		a.push_back(0);
		a.insert(a.end(),p.begin(),p.end());
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				a.push_back(p[i]+p[j]);
		sort(a.begin(),a.end());
		a.erase(unique(a.begin(),a.end()),a.end());
		
		int res=0;
		for(int i=0;i<a.size();i++){
			int j=upper_bound(a.begin(),a.end(),m-a[i])-a.begin();
			if(j)
				res=max(res,a[i]+a[j-1]);
		}
		cout<<res<<endl;
	}
	
	return 0;
}

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

int P[1001], mem[1001 * 1001], N, M;
int main()
{
loop:
  cin >> N >> M;
  if(!N)
    return 0;
  N++;
  P[0] = 0;
  for(int i = 1; i < N; i++)
    cin >> P[i];
  for(int i = 0; i < N; i++)
    for(int j = 0; j < N; j++)
      mem[N * i + j] = P[i] + P[j];
  sort(mem, mem + N * N);

  int ans = 0;
  for(int i = 0; i < N * N; i++)
  {
    if(mem[i] > M)
      continue;
    ans = max(ans, mem[i] + *(upper_bound(mem, mem + N * N, M - mem[i]) - 1));
  }
  cout << ans << endl;
  goto loop;
}

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

#define loop(i,n) for(int i=1;i<=n;i++)
int n,m;
int p[1002];
set<int> s1;
set<int> s2;

signed main(){
  cin>>n>>m;
  loop(i,n)cin>>p[i];
  s1.insert(0);
  loop(a,4){
    for(int x:s1){
      s2.insert(x);
      loop(i,n){
        s2.insert(x+p[i]);
      }
    }
    swap(s1,s2);
  }
  int mx=0;
  for(int x:s1){
    if(x<=m)mx=max(x,mx);
  }
  cout<<mx<<endl;
  return 0;
}

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

using namespace std;

int a[2000];

int main(){
	
	vector<int> v;
	vector<int>::iterator ite;
	
	int m, n;
	
	while(true){
		
		cin >> n >> m;
		
		if(n == 0 && m == 0){
			break;
		}
		
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		
		a[n] = 0;
		
		v.erase(v.begin(), v.end());
		
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				v.push_back(a[i] + a[j]);
			}
		}
		
		sort(v.begin(), v.end());
		
		int ans = 0;
		
		for(int i = 0; i < (n + 1) * (n + 1); i++){
			if(v.at(i) > m){
				break;
			}
			ite = upper_bound(v.begin(), v.end(), m - v.at(i));
			ite--;
			if(*ite + v.at(i) > ans){
				ans = *ite + v.at(i);
			}
		}
		
		printf("%d\n", ans);
		
	}
	return 0;
}