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*len(arr)+j] = arr[i] + arr[j]
			}
		}

		sort.Slice(sums, func(i, j int) bool { return sums[i] < sums[j] })
		var m int64 = 0
		for _, val := range sums {
			rem := M - val
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, val+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

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

int n, m, point1[1001], sum, ans;
vector<int> point2;

int solve(int start, int end, int border){
    int mid;
    while(1){
        if((end + start) % 2){
            mid = (end + start) / 2 + 1;
        } else{
            mid = (end + start) / 2;
        }

        if(point2[mid] > border){
            end = mid;
        } else{
            start = mid;
        }

        if(end - start == 1){
            if(point2[end] < border){
                return point2[end];
            } else{
                return point2[start];
            }
        }
    }
}

int main(){
    while(cin >> n >> m && n && m){
        for(int i = 0; i < n; i++){
            cin >> point1[i];
        }
        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < n + 1; j++){
                point2.push_back(point1[i] + point1[j]);
            }
        }

        sort(point1, point1 + n + 1);
        sort(point2.begin(), point2.end());

        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < n + 1; j++){
                sum = point1[i] + point1[j];
                if(sum > m){
                    continue;
                }
                ans = max(ans, sum + solve(0, point2.size() - 1, m - sum));
            }
        }

        cout << ans << endl;

        memset(point1, 0, sizeof(point1));
        point2.clear();
        ans = 0;
    }

    return 0;
}

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

using namespace std;

int main(void){
    int c[1001001],p[1010],n,m,cnt,i,j,tmp,ans,pnt;
    int *point;
    cin >> n >> m;
    while(n!=0 && m!=0){
        cnt = 0;
        ans = 0;
        p[0]=0;
        for(i=1; i<=n; i++){
            cin >> p[i];
        }
        m++;
        for(i=0; i<=n; i++){
            for(j=i; j<=n; j++){
                if(p[i]+p[j]<m){
                    c[cnt] = p[i]+p[j];
                    cnt++;
                }
            }
        }
        sort(c,c+cnt);
        for(i=0; i<cnt; i++){
            point = lower_bound(c,c+cnt,m-c[i]);
            pnt = (int)(point-c);
            if(pnt>0){
                pnt--;
                tmp = c[i]+c[pnt];
                ans = max(ans,tmp);
            }
        }
        cout << ans <<endl;
        cin >> n >> m;
    }
    return 0;
}

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

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(n);
		for (int i = 0; i < n; i++) {
			cin >> data[i];
		}
		sort(data.begin(), data.end());
		long long int a = m;
		for (int i = 0; i < 4; i++) {
			vector<long long int>::iterator ite = upper_bound(data.begin(), data.end(), a);
			if (ite != data.begin()) {
				a -= *(ite-1);
			}
		}
		cout << m-a << endl;
	}
	return 0;
}

C++: 4 50
3
14
15
9
3 21
16
11
2

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;
  set<int>::iterator it, it2, it3;
  while ( cin >> n >> m && n && m ) {
    set<int> p;
    for ( int i = 0; i < n; i++ ) {
      int num;
      cin >> num;
      p.insert( num );
    }
    p.insert( 0 );
    set<int> two_selected;
    for ( it = p.begin(); it != p.end() ; ++it ) {
      for ( it2 = p.begin(); it2 != p.end(); ++it2 ) {
        two_selected.insert( *it +*it2 );
      }
    }
    int ans = 0;
    for ( it = p.begin(); it != p.end(); ++it ) {
      for ( it2 = p.begin(); it2 != p.end(); ++it2 ) {
        it3 = two_selected.upper_bound( m - ( *it + *it2 ) );
        if ( it3 != two_selected.begin() ) {
          --it3;
          ans = max( ans, *it + *it2 + *it3 );
        }
      }
    }
    PRINT( ans );
  }
	return 0;
}

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

int main() {

	int N, M;
	
	while(cin >> N >> M, N + M > 0) {
	
		int P[1111];
		vector<int> PP;
		
		for(int i= 0; i < N; i++) {
			cin >> P[i];
		}
		
		P[N++] = 0;
		
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < N; j++) {
				PP.push_back(P[i] + P[j]);
			}
		}
		
		sort(PP.begin(), PP.end());
		
		int ans = 0;;
		
		for(int i = 0, m; i < PP.size(); i++) {
			m = M - PP[i];
			if(m < 0) continue;
			int it = *(upper_bound(PP.begin(), PP.end(), m) - 1);
			ans = max(ans, PP[i] + it );
			//cout << PP[i] << " " << it << endl;
		}
		
		cout << ans << endl;
	}

	return 0;

}