Go: ///
// File:  0529.go
// Author: ymiyamoto
//
// Created on Wed Feb 28 20:39:28 2018
//
package main

import (
	"fmt"
	"sort"
)

func max(n, m int64) int64 {
	if n < m {
		return m
	} else {
		return n
	}
}

func main() {
	for {
		var N int
		var M int64
		fmt.Scan(&N, &M)
		if N == 0 && M == 0 {
			break
		}

		arr := make([]int64, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })

		sums := make([]int64, 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.Slice(sums, func(i, j int) bool { return sums[i] < sums[j] })

		var m int64 = 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 "array"
#include "cmath"
#include "complex"
#include "cstdio"
#include "deque"
#include "initializer_list"
#include "map"
#include "numeric"
#include "queue"
#include "regex"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "vector"

using std::accumulate;
using std::all_of;
using std::any_of;
using std::array;
using std::binary_search;
using std::copy;
using std::count_if;
using std::deque;
using std::fill;
using std::find_if;
using std::for_each;
using std::inner_product;
using std::lower_bound;
using std::map;
using std::max;
using std::min;
using std::multimap;
using std::multiset;
using std::next_permutation;
using std::none_of;
using std::priority_queue;
using std::queue;
using std::regex;
using std::search;
using std::set;
using std::sort;
using std::sregex_token_iterator;
using std::stable_sort;
using std::stack;
using std::string;
using std::swap;
using std::to_string;
using std::transform;
using std::unordered_map;
using std::upper_bound;
using std::vector;

#define INF 1000000000 // 10^10
#define FOR(i, n) for ((i) = 0; (i) < (n); ++(i))
#define FOR_FROM_NOT_ZERO(i, s, n) for ((i) = (s); (i) < (n); ++(i))
#define FOR_dec(i, n) for ((i) = (n); (i) >= 0; --(i))
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define d2_arr(height, width) array<array<int, (width)>, (height)>
#define d2_vec vector<vector<int>>
#define in_int(x) scanf(" %d/n", &(x));
#define in_str(x) scanf(" %[a-zA-Z 0-9_]/n", (x));
#define ch(x) printf("debag %d\n", (x))

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dx_ar[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy_ar[] = {1, 0, -1, 1, -1, 1, 0, -1};

template <typename type> void print_array_vertical(type d1_arr);
template <typename type> void print_array_horizon(type d1_arr);
template <typename type> void print_2darray(type d2_arr);
vector<vector<int>> d2_zeros(int height, int width);
vector<vector<int>> padding(vector<vector<int>> arr, int pad);
inline vector<int> str_int_split(char *s);
inline std::vector<std::vector<int>> in_d2_arr(int N, int size = 1000);
inline std::vector<int> in_d1_arr(int size = 1000);
inline double d2_distance(int x1, int y1, int x2, int y2);
inline int d2_manhattan(int x1, int y1, int x2, int y2);

int main() {
  int i, N, M, temp;
  std::vector<int> v = in_d1_arr();
  N = v[0];
  M = v[1];
  std::vector<int> tar;

  FOR(i, N) {
    in_int(temp);
    tar.push_back(temp);
  }

  sort(tar.begin(), tar.end(), std::greater<int>());
  int point = 0;

  FOR(i, 4) {
    FOR(i, N) {
      if (point + tar[i] <= M) {
        point = point + tar[i];
        break;
      }
    }
  }

  printf("%d\n", point);

  return 0;
}

template <typename type> void print_array_vertical(type d1_arr) {
  int i;
  string s;
  FOR(i, d1_arr.size()) {
    printf("%d\n", d1_arr[i]);
    fflush(stdout);
  }
}

template <typename type> void print_array_horizon(type d1_arr) {
  int i;
  FOR(i, d1_arr.size() - 1) { printf("%d ", d1_arr[i]); }
  printf("%d\n", d1_arr.back());
  fflush(stdout);
}

template <typename type> void print_2darray(type d2_arr) {
  int i, j;
  FOR(i, d2_arr.size()) {
    FOR(j, d2_arr[i].size() - 1) { printf("%d ", d2_arr[i][j]); }
    printf("%d\n", d2_arr[i].back());
    fflush(stdout);
  }
}

vector<vector<int>> d2_zeros(int height, int width) {
  vector<vector<int>> temp;
  temp.resize(height);
  int i, j;
  FOR(i, height) {
    temp[i].resize(width);
    FOR(j, width) { temp[i][j] = 0; }
  }
  return temp;
}

vector<vector<int>> padding(vector<vector<int>> arr, int pad) {
  vector<vector<int>> arr_c = arr;
  int height = arr_c.size();
  int width = arr_c[0].size();
  std::vector<int> v;
  v.resize(width);
  fill(v.begin(), v.end(), pad);
  arr_c.insert(arr_c.begin(), v);
  arr_c.push_back(v);
  int i;
  FOR(i, height + 2) {
    arr_c[i].insert(arr_c[i].begin(), pad);
    arr_c[i].resize(width + 2);
  }
  arr_c.shrink_to_fit();
  return arr_c;
}

inline vector<int> str_int_split(char *s) {
  char *tok = strtok(s, " ");
  std::vector<int> v;
  while (tok != NULL) {
    v.push_back(atoi(tok));
    tok = strtok(NULL, " "); /* 2回目以降 */
  }
  return v;
}

inline std::vector<std::vector<int>> in_d2_arr(int N, int size) {
  int i;
  std::vector<std::vector<int>> v;
  FOR(i, N) {
    char *lines;
    lines = new char[size];
    in_str(lines);
    v.push_back(str_int_split(lines));
    delete[] lines;
  }
  return v;
}

inline std::vector<int> in_d1_arr(int size) {
  std::vector<int> v;
  char *lines;
  lines = new char[size];
  in_str(lines);
  v = str_int_split(lines);
  delete[] lines;
  return v;
}

inline double d2_distance(int x1, int y1, int x2, int y2) {
  return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
}

inline int d2_manhattan(int x1, int y1, int x2, int y2) {
  return (abs(x2 - x1) + abs(y2 - y1));
}

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

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const LL LINF = 1LL << 60;
const LL MOD = 100000;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define all(c) (c).begin(), (c).end()

struct edge{ LL to, cost; edge(int t, LL c) :to(t), cost(c){} };

int dxk[] = { 1, 1, 1, 0, -1, 0 };
int dxg[] = { 0, 1, 0, -1, -1, -1 };
int dy[] = { -1, 0, 1, 1, 0, -1 };

int main()
{
	int n;
	LL m;
	while (cin >> n >> m && n)
	{
		vector<LL> nums(n);
		rep(i, n) cin >> nums[i];
		vector<LL> arr(n*n + 2);
		rep(i, n) rep(j, n)
		{
			arr[i * n + j] = nums[i] + nums[j];
		}
		rep(i, n) arr[n*n + 1] = nums[i];
		sort(all(arr));

		LL ans = 0;
		for (auto x : arr)
		{
			LL rem = m - x;
			auto it = upper_bound(all(arr), rem);
			if (it != arr.begin())
			{
				LL num = *(--it);
				ans = max(ans, num + x);
			}
		}
		cout << ans << endl;
	}
	return 0;
}