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<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<random>
#include<functional>
#include<utility>

typedef long long ll;

#define rep(i,n) for(int i=0; i<n; i++)
#define pb push_back
#define P pair<int, int>
#define PLI pair<ll, int>
#define PLL pair<ll, ll>
#define VI vector<int>
#define VII vector<VI>

using namespace std;


int N, M;
VI p, sum;
int main() {
	while (cin >> N >> M, N)
	{
		p = VI(N+1);
		sum.clear();
		p[0] = 0;
		rep(i, N) cin >> p[i+1];

		sort(p.begin(), p.end());
		rep(i, N+1) {
			for (int j = i; j < N+1; j++) {
				if (p[i] + p[j] > M) break;
				sum.push_back(p[i] + p[j]);
			}
		}

		int ans = 0;
		sort(sum.begin(), sum.end());
		for (auto& it : sum) {
			if (it > M / 2) break;
			auto an = upper_bound(sum.begin(), sum.end(), M - it) - 1;
			int tmp = it + *an;
			ans = max(ans, tmp);
		}

		cout << ans << endl;
	}

	return 0;
}

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

#define NMAX 1000
using namespace std;

int N;
long long M;
long long  sc[NMAX];

int solve(long long s,int d){
	int m=-1;
	if(s>M) return 0;
	if(d==4) return s;
	for(int i=0;i<N;i++){
		m=max(m,solve(s+sc[i],d+1));
	}
	m=max(m,solve(s,d+1));
	return m;
}

int main(void){
	std::ios_base::sync_with_stdio(false);

	while(1){

		cin>>N>>M;
		if(N==0&&M==0) break;

		for(int i=0;i<N;i++){
			cin>>sc[i];
		}

		cout<<solve(0,0)<<endl;
	}

	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n >> m && n && m){
    vary(ll,s,n,0);
    rep(i,n){
      cin >> s[i];
    }
    sort(all(s));
    vector<vector<ll>> dp(5,vector<ll>(n+1,0));
    rep(i,4){
      rep(j,n){
        rep(k,n){
          if(dp[i][j] + s[k] <= m){
            dp[i+1][j] = max(dp[i+1][j],dp[i][j]+s[k]);
          }
        }
      }
    }
    rep(i,5){
      rep(j,n){
        cout << dp[i][j] << " ";
      }
      cout << endl;
    }
    ll ans = 0;
    rep(i,5){
      rep(j,n){
        ans = max(ans,dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef vector<ll> vll;

signed main() {
    int N, M;
    while (cin >> N >> M && N) {
        vi P(N + 1);
        rep(i, N) cin >> P[i];
        P[N] = 0;

        vi a;
        rep(i, N + 1) rep(j, i + 1) {
            if (P[i] + P[j] <= M) a.emplace_back(P[i] + P[j]);
        }

        sort(all(a));

        int ma = 0;
        int l = 0;
        int r = a.size() - 1;
        while (l <= r) {
            while (a[l] + a[r] > M) r--;
            ma = max(ma, a[l] + a[r]);
            l++;
        }

        cout << ma << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
 
using namespace std;
 
#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define SQ(x) ((x)*(x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1<<29)
#define EPS 1e-10
#define MOD 1000000007
 
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
 
int N;
ll M;
ll P[1024];
 
int main(){
  while(scanf("%d%lld",&N,&M) && N+M){
    ll res = 0;
    rep(i,N) scanf("%lld", P+i);
    P[N] = 0;
    vector<ll>PP;
    rep(i,N+1) rep(j,N+1) PP.push_back(P[i]+P[j]);
    sort(PP.begin(), PP.end());
    PP.erase(unique(PP.begin(), PP.end()), PP.end());
    for(int i=0;i<PP.size();i++){
      if(PP[i] > M) continue;
      ll x = M-PP[i];
      ll last = *(--upper_bound(PP.begin(), PP.end(), x));
      if(PP[i]+last <= M) res =  max(res, PP[i]+last);
    }
     
    printf("%lld\n", res);
  }
  return 0;
}

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

using namespace std;
typedef long long ll;

int main()
{
    while (1) {
        int N;
        ll M;
        scanf("%d %lld", &N, &M);
        if (N == 0 && M == 0) {
            break;
        }
        set<ll> P;
        for (int n = 0; n < N; ++n) {
            ll tmp;
            cin >> tmp;
            P.insert(tmp);
        }

        set<ll> setP2;
        setP2.insert(0);
        for (auto it1 = P.begin(); it1 != P.end();  ++it1) {
            if (*it1 <= M) setP2.insert(*it1);
            else break;
            for (auto it2 = it1; it2 != P.end(); ++it2) {
                if (*it1 + *it2 <= M) setP2.insert(*it1 + *it2);
                else break;
            }
        }

        ll Res = 0;
        auto it1 = setP2.begin();
        auto it2 = setP2.rbegin();
        for (; it1 != setP2.end(); ++it1) {
            ll S;
            while ((S = *it1 + *it2) > M && it2 != setP2.rend()) {
                ++it2;
            }
            if (S <= M && Res < S) {
                Res = S;
            }
            if (*it1 > *it2) break;
        }
        cout << Res << endl;
    }

    return 0;
}