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 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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, p[1010];
int dp[1010];

int main(){
	while (cin >> n >> m&&n + m){
		/*fill(dp, dp + 1010, 0);
		p[0] = 0;
		FOR(i, 1, n + 1)cin >> p[n];
		REP(i, 4){
			REP(j, n + 1){
				int now = dp[j];
				REP(k, n + 1){
					if (dp[j] + p[k] <= m){
						
					}
				}
			}
		}*/
		REP(i, m){}
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
   
int n, m;
vector<int> p1, p2;
int main()
{
  while(true){
    scanf("%d%d", &n, &m);
    if(!n && !m)
      break;
  
    p1.clear();
    p2.clear();
       
    //chap1
    p1.push_back(0);
    for(int i = 0; i < n; i++){
      int p;
      scanf("%d", &p);
      p1.push_back(p);
    }
   
    //chap2
    for(int i = 0; i <= n; i++)
      for(int j = i; j <= n; j++)
    p2.push_back(p1[i] + p1[j]);
   
    sort(p2.begin(), p2.begin() + p2.size());
   
    //chap3
    int ans = 0;
    for(int i = 0; i < p2.size(); i++){
      vector<int>::iterator count2 = upper_bound(p2.begin(), p2.end(), m - p2[i]);
      if(count2 != p2.begin())
    ans = max(ans, *(count2 - 1) + p2[i]);
    }
    printf("%d", ans);
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;

int solve(vector<int> p, int n, int m) {
    vector<int> cd(n * n);
    for (int l = 0; l < n; ++l) {
        for (int i = 0; i < n; ++i) {
            cd[n * l + i] = p[l] + p[i];
        }
    }

    sort(cd.begin(), cd.end());

    int mx = -1;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int r = m - (p[i] + p[j]);
            auto it = upper_bound(cd.begin(), cd.end(), r);
            if(*(--it) + p[i] + p[j] <= m )  {
                mx = max(mx, *it + p[i] + p[j]);
            }
        }
    }
    return mx;
}
int main() {
    while(true) {
        int n,m;
        cin >> n >> m;
        if(!n && !m) break;

        vector<int> p(n + 1);
        for (int i = 0; i < n; ++i) {
            cin >> p[i];
        }
        p[n] = 0;

        int res = solve(p, n + 1, m);
        cout << res << endl;
    }

    return 0;
}

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

set<long long> sep;
long long ary[1010];
int main(){
	int N;
	long long M,hog,res;
	set<long long>::iterator it,it2;
	while(1){
		sep.clear();
		cin>>N>>M;
		if(N==0&&M==0) break;
		for(int i=0;i<N;i++){
			cin>>ary[i];
		}
		ary[N]=0;
		for(int i=0;i<=N;i++){
			for(int j=i;j<=N;j++){
				if(ary[i]+ary[j]<=M){
					sep.insert(ary[i]+ary[j]);
				}
			}
		}
		res=0;
		it2=sep.end();
		it2--;
		for(it=sep.begin();it!=sep.end();it++){
			while((*it)+(*it2)>M)it2--;
			res=max(res,(*it)+(*it2));
		}
		cout<<res<<endl;

	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
typedef long long L;
using namespace  std;
int main(){
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
	int n;
	L m,p[1001],sum=0;
	vector<L> sum1;
	vector<L>::iterator it,ele;
	scanf("%d %lld",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%lld",&p[i]);
	}
	p[n]=0;
	for(int i=0;i<=n;i++){
		for(int i2=0;i2<=n;i2++){
			sum1.push_back(p[i]+p[i2]);
		}
	}
	sort(sum1.begin(),sum1.end());
	for(it=sum1.begin();it<sum1.end();it++){
		if(*it>m)
			break;
		ele=upper_bound(sum1.begin(),sum1.end(),(m-(*it) ) );
		if(ele!=sum1.begin()){
			ele--;
		}
		if(sum<=(*it)+(*ele)){
			sum=(*it)+(*ele);
		}
		if(sum==m)
			break;
	}
	printf("%lld\n",sum);
	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(6,vector<ll>(n+1,0));
    rep(i,4){
      rep(k,n){
        rep(j,n){
          if(dp[i][j] + s[k] <= m){
            dp[i+1][k] = max(dp[i+1][k],dp[i][j]+s[k]);
          }
          else{
            dp[i+1][k] = max(dp[i+1][k],dp[i][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;
}