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 <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#define fi "darts.inp"
#define fo "darts.out"
using namespace std;
long long n,m,a[100011],ans;
vector<long long > vt;
int main()
{
//    freopen (fi , "r" ,stdin);
//    freopen (fo , "w" ,stdout);
    while (1)
    {
    scanf("%lld%lld",&n,&m);
    if (n==0&&m==0) return 0;
    for (int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    ans=0;
    n++;
    a[n]=0;
    vt.clear();
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            vt.push_back(a[i]+a[j]);
        }
    sort(vt.begin(),vt.end());
    int c;
    c=vt.size()-1;
    for (int i=0;i<vt.size();i++)
    {
        while (c>=0&&vt[c]+vt[i]>m)
            c--;
        if (vt[c]+vt[i]<=m) ans=max(ans,vt[c]+vt[i]);
    }
    cout<<ans<<endl;
    }
    return 0;
}

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

using namespace std;
int main(){
	int n,m,ans;
	int dp[4][1001];
	for(;;){
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)break;
		for(int i=0;i<1001;i++)for(int j=0;j<4;j++)dp[j][i]=0;
		for(int i=0;i<n;i++)scanf("%d",&dp[0][i]);
		sort(dp[0],dp[0]+n);
		ans = 0;
		for(int i=0;i<n;i++){
			for(int j=1;j<4;j++){
				for(int k=0;k<n;k++){
					int t = dp[0][i]+dp[j-1][k];
					if(t<=m)dp[j][k] = max(dp[j][k], t);
				}
			}
		}
		for(int i=0;i<4;i++){
			for(int j=0;j<n;j++){
				ans = max(ans,dp[i][j]);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
    int n;
    ll m;
    while(cin>>n>>m,n){
        vll v(n);
        rep(i,n){
            cin>>v[i];
        }
        sort(ALL(v));
        set<ll> se;
        se.insert(0LL);
        rep(i,n){
            se.insert(v[i]);
        }
        rep(i,n){
            rep(j,n){
                se.insert(v[i]+v[j]);
            }
        }
        vll x;
        for(ll t:se){
            x.PB(t);
        }
        sort(ALL(x));
        ll ans=0;
        rep(i,x.size()){
            ll t=m-x[i];
            if(t<0) continue;
            auto it=upper_bound(ALL(x),t);
            if(it==x.begin()) continue;
            it--;
            maxs(ans,*it + x[i]);
        }
        cout<<ans<<endl;
    }
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
      cout<<fixed<<setprecision(20);
    mainmain();
}

C++: #include<bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
int main(void){
    ll n, m, p[1000010];
    while(cin>>n>>m){
        if(n+m==0) break;
        p[0]=0;
        ll maxp=0;
        for(ll i=1; i<=n; i++){
            cin >> p[i];
        }
        ll now=n+1;
        for(ll i=0; i<=n; i++){
            for(ll j=i; j<=n; j++){
                p[now++]=p[i]+p[j];
            }
        }
        sort(p, p+now);
        ll ans=0;
        for(int i=0; i<now; i++){
        ll maxa=m-p[i];
        if(maxa<0) continue;
        ll under=0, upper=now;
        while(upper-under>0){
            ll mid=(under+upper)/2;
            if(maxa>p[mid]){
                under=mid+1;
            }
            else if(maxa==p[mid]){
                break;
            }
            else{
                upper=mid;
            }
        }
        ans=max(ans, p[i]+p[upper-1]);
        }
        cout << ans << "\n";
         
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
	int n, m;
	int p;
	vector<int> g;
	while (1){
		cin >> n >> m;
		if (n == 0 && m == 0)break;
		for (int i = 0; i < n; i++){
			cin >> p;
			g.push_back(p);
		}
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){
				g.push_back(g[i] + g[i2]);
			}
		}
		int x, y;
		x = 0; y = g.size() - 1;
		int maxv = 0;
		sort(g.begin(), g.end());
		while (1){
			if (g[x] + g[y] > maxv&&g[x] + g[y] <= m)maxv = g[x] + g[y];
			if (g[x] + g[y] <= m)x++;
			if (g[x] + g[y] > m)y--;
			if (x > y)break;
		}
		cout << maxv << endl;
		g.clear();
	}
}