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 <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;
}

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

#define for_(i,a,b) for(int i=a;i<b;++i)
#define for_rev(i,a,b) for(int i=a;i>=b;--i)
#define allof(a) a.begin(),a.end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) (int)a.size()

int N, M;
int p[1010];

int main() {
	while (cin >> N >> M, N) {
		for_(i,0,N) cin >> p[i];
	
		vector<int> vp;
		vp.push_back(0);
		for_(i,0,N) vp.push_back(p[i]);
	
		vector<int> sum2;
		for_(i,0,N + 1) for_(j,0,N + 1) sum2.push_back(vp[i] + vp[j]);
	
		sort(allof(sum2));
		int s_size = size_of(sum2);
		int ans = 0;
		for_(i,0,s_size) {
			int sum = sum2[i];
			if (sum <= M) {
				int sub = M - sum;
				int lb = *(upper_bound(allof(sum2), sub) - 1);
				//cout << sub << " " << lb << endl;
				sum += lb;
				ans = max(ans, sum);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

int n,m;
long long target[1000];
int sum = 0;

void chek(int cnt, int point,int now)
{
	int i;
	//int sum=0;
	sum = max(sum, point);
	if (cnt == 5)
	{
		return ;
	}
	if (sum == m)
	{
		return;
	}

	for (i = now; i <= n; i++)
	{
		if (point + target[i] <= m)
		{
			chek(cnt + 1, point + target[i],i);
		}
		else return;
	}

}

int main()
{
	//int sum;
	int i;

	while (scanf("%d %d",&n,&m), n != 0 || m != 0)
	{
		sum = 0;
		//printf("AA%d\n",m);
		for (i = 1; i <= n; i++)
		{
			scanf("%d", &target[i]);
		}

		sort(target + 1, target + n + 1);

		chek(1, 0,0);

		printf("%d\n", sum);
	}

	return 0;
}