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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector<long long int> data;
		long long int b;
		data.push_back(0);
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				data.push_back(b);
			}
		}
		sort(data.begin(), data.end());
		vector<long long int> data2;
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		for (long long int i = 0; i < data2.size(); i++) {
			long long int a = data2[i];
			data.push_back(a);
			for (long long int j = i+1; j < data2.size(); j++) {
				if (data2[j] == a) {
					i++;
				} else {
					break;
				}
			}
		}
		data2.clear();
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		long long int sz = data2.size();
		cout << data2[sz-1] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; ++i)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define SCAN(x) cin >> x
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define PB push_back
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<long double> P;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> Point;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

#ifdef WIN32
#define dump(x) cerr << #x << " = " << (x) << "\n"
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif

template<class T> ostream &operator<<( ostream &os, vector<T> &v ) {
	for ( int i = 0; i < v.size(); i++ ) {
		if ( i ) os << " ";
		os << v[ i ];
	}
	return os;
}

int main() {
	ios::sync_with_stdio( false );
	cin.tie( 0 );
	int n, m;
  VI p;

  int ans = 0;
  set<int> two_selected;
  while ( cin >> n >> m && n && m ) {
    p.resize( n );
    for ( int i = 0; i < n; i++ ) {
      cin >> p[ i ];
    }
    p.push_back( 0 );
    for ( int i = 0; i < n + 1; i++ ) {
      for ( int j = 0; j < n + 1; j++ ) {
        two_selected.insert( p[ i ] + p[ j ] );
      }
    }
    ans = 0;
    for ( int i = 0; i < n + 1; i++ ) {
      for ( int j = 0; j < n + 1; j++ ) {
        set<int>::iterator it = two_selected.upper_bound( m - ( p[ i ] + p[ j ] ) );
        if ( it != two_selected.begin() ) {
          --it;
          ans = max( ans, p[ i ] + p[ j ] + *it );
        }
      }
    }
    PRINT( ans );
    two_selected.clear();
  }
	return 0;
}

C++: #include "stdio.h"
#include <iostream>
#include <algorithm>

using namespace std;

long long int gten[1000000];
long long int p[1111];

int bake[22222222];

long long int ans[1000];

int main()
{
  int ww = 0;
  while(1)
    {
      long long int n,m;
      scanf("%lld %lld",&n,&m);
      if(n == 0 && m == 0)
	{
	  break;
	}
      p[0] = 0;
      for(int i = 1; i <= n; i++)
	{
	  scanf("%lld",&(p[i]));
	}
      for(int i = 0; i <= m; i++)
	{
	  bake[i] = 0;
	}
      for(int i = 0; i <= n; i++)
	{
	  for(int ii = 0; ii <= n; ii++)
	    {
	      if(p[i] + p[ii] <= m)
		{
		  bake[p[i] + p[ii]] = 1;
		}
	    }
	}
      int nw = 0;
      for(int i = 0; i <= m; i++)
	{
	  if(bake[i] == 1)
	    {
	      gten[nw] = i;
	      nw++;
	    }
	}
      long long int max = 0;
      int ai = 0;
      for(int i = nw - 1; i >= 0; i--)
	{
	  while(1)
	    {
	      if(ai + 1 >= nw)
		{
		  break;
		}
	      if(gten[i] + gten[ai + 1] <= m)
		{
		  ai++;
		  if(max < gten[i] + gten[ai])
		    {
		      max = gten[i] + gten[ai];
		    }
		}
	      else
		{
		  break;
		}
	    }
	}
      ans[ww] = max;
      ww++;
    }
  for(int i = 0; i < ww; i++)
    {
      printf("%lld\n",ans[i]);
    }
  return 0;
}

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

int main() {
	std::int64_t n, m;
	std::vector<std::int64_t> in, pairSum;
	in.reserve(1000);
	pairSum.reserve(1000 * 1000);

	while (std::cin >> n >> m, n + m != 0) {
		in.push_back(0);
		for (int i = 0; i < n; ++i) {
			int p;
			std::cin >> p;
			in.push_back(p);
		}

		for (auto x : in)
			for (auto y : in)
				pairSum.push_back(x + y);

		std::sort(pairSum.begin(), pairSum.end());

		std::int64_t ans = 0;
		for (auto e : pairSum) {
			if (e > m)
				continue;

			auto it = std::upper_bound(pairSum.begin(), pairSum.end(), m - e);
			--it;
			ans = std::max(ans, e + *it);
		}

		std::cout << ans << std::endl;

		in.clear();
		pairSum.clear();
	}
}

C++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>
using namespace std;


int main(){
	
	int n,m;
	while(1){
		cin>>n>>m;
		if(!n&&!m)break;
		int g[1009]={};
		int w=2000000009;
		for(int i=0;i<n;i++)
		{cin>>g[i];w=min(w,g[i]);}
		sort(g,g+n);
		reverse(g,g+n);
		long long int ans=0,H=0;;
		int y=0;
		int r=3;
		int i=0;
		if(w*4>=m){int p=m/w;ans=p*w;}
		else{
		while(i<4){
			
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
			if(y==n)break;
		}
		}
	
		y=0;
		H=max(H,ans);
		r=2;i=0;ans=0;
		if(w*3>=m){int p=m/w;ans=p*w;}
		else{
		while(i<3){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
			if(y==n)break;
		}
		}
		y=0;
		H=max(H,ans);
		r=1;i=0;ans=0;
		if(w*2>=m){int p=m/w;ans=p*w;}
		else{
		while(i<2){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
			if(y==n)break;
		}
		}
		y=0;
		H=max(H,ans);
		r=0;i=0;ans=0;
		if(w>=m){int p=m/w;ans=p*w;}
		else{
		while(i<1){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
			if(y==n)break;
		}
		}
		H=max(H,ans);


		cout<<H<<endl;
	}
	


	return 0;
}