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 int long long

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> plli;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#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);i>=0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()

const ll mod = 1e9 + 7;
const ll INF = 1<<30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n, m;

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    while (n != 0) {
        int p[1005] = {};
        rep(i, n) cin >> p[i];
        vector<int> score;
        score.push_back(0);
        score.push_back(1e12);
        rep(i, n) score.push_back(p[i]);
        rep(i, n) rep(j, n) {
            score.push_back(p[i] + p[j]);
        }
        sort(all(score));
        score.erase(unique(all(score)), score.end());
        int ans = 0;
        rep(i, score.size()) {
            if (score[i] > m) break;
            int l = 0;
            int r = score.size() + 1;
            while (r - l > 1) {
                int mid = (r + l) / 2;
                if (score[i] + score[mid] <= m) l = mid;
                else r = mid;
            }
            int idx = l;
            if (score[i] + score[idx] > m) continue;
            ans = max(ans, score[i] + score[idx]);
        }
        cout << ans << endl;
        cin >> n >> m;
    }
}

C++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>
using namespace std;


int main(){
	
	int n,m;
	while(1){
		cin>>n>>m;
		if(!n&&!m)break;
		int g[1009]={};
		int w=2000000009;
		for(int i=0;i<n;i++)
		{cin>>g[i];w=min(w,g[i]);}
		sort(g,g+n);
		reverse(g,g+n);
		long long int ans=0,H=0;;
		int y=0;
		int r=3;
		int i=0;
		while(i<4){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
		}
		y=0;
		H=max(H,ans);
		r=2;i=0;ans=0;
		while(i<3){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
		}
		
		y=0;
		H=max(H,ans);
		r=1;i=0;ans=0;
		while(i<2){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
		}
		
		y=0;
		H=max(H,ans);
		r=0;i=0;ans=0;
		while(i<1){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
		}
		H=max(H,ans);


		cout<<H<<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 <set>
#include <algorithm>
#include <iostream>
#define MAX(a,b) (((a)>(b))?(a):(b))
using namespace std;
typedef long long int lli;

set<lli> v;
lli m,n;
lli k;
int mx;

int dfs(int num,int score,int z){
	mx=MAX(mx,score);
	if(num==z)return 1;
	if(num<z){
		for(set<lli>::reverse_iterator it=v.rbegin();it!=v.rend();it++){
			if(score+*it<=m){
				if(dfs(num+1,score+*it,z)){
					return 1;
				}
			}
		}
	}
	return 0;
}

int main(void){
	while(cin>>n>>m,(n||m)){
		v.clear();
		mx=0;
		for(lli i=0;i<n;i++){
			cin>>k;
			v.insert(k);
		}
//				cout<<"a:";
//				for(set<lli>::reverse_iterator it=v.rbegin();it!=v.rend();it++) cout<<*it<<" ";
//				cout<<endl;

		for(int f=1;f<5;f++){
			for(set<lli>::reverse_iterator itm=v.rbegin();itm!=v.rend();itm++){
				if(*itm<=m) dfs(1,*itm,f);
			}
		}
		cout<<mx<<endl;
	}
	return 0;
}

C++: #include<iostream>

#define NMAX 1000
#define MMAX 200000
using namespace std;

int main(void){

	std::ios_base::sync_with_stdio();

	int N,M;
	int max=-1;
	int dp[4][MMAX]={-1};
	int sc[NMAX];

	while(1){
		cin>>N>>M;
		if(N==0&&M==0) break;
		for(int i=0;i<N;i++){
			cin>>sc[i];
		}
		for(int i=0;i<4;i++){
			for(int j=0;j<MMAX;j++){
				dp[i][j]=-1;
			}
		}
		dp[0][0]=1;
		for(int j=0;j<=M;j++){
			for(int k=0;k<4;k++){
				if(dp[k][j]==1){
					for(int l=0;l<N;l++){
						if(j+sc[l]<=M){
							dp[k+1][j+sc[l]]=1;
							if(max<j+sc[l]) max=j+sc[l];
						}
					}
				}
			}
		}
		cout<<max<<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));
    queue<ll> q;
    q.push(0);
    ll k = 4;
    ll ma = 0;
    while(k){
      auto S = q.size();
      rep(l,S){
        ll a = q.front();q.pop();
        ma = max(a,ma);
        rep(i,n){
          if(a + s[i] <= m)
            q.push(a+s[i]);
        }
      }
      --k;
    }
    while(q.size()){
      ma = max(ma,q.front());
      q.pop();
    }
    cout << ma <<endl;
  }
  return 0;
}