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 <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(n+2,vector<ll>(m+2,inf));
    rep(i,m+1){
      dp[0][i] = 0;
    }
    rep(i,n+1){
      dp[i][0] = 0;
    }
    REP(i,0,n){
      for(ll j = 0; j < m+1 + s[i]; j += s[i]){
        if(j - s[i] >= 0){
          dp[i+1][j] = min(dp[i+1][j],dp[i][j-s[i]]+1);
        }
      }
      if(i > 0){
        rep(j,m+1){
          dp[i+1][j] = min(dp[i][j],dp[i+1][j]);
        }
      }
    }
    ll ans = 0;
    for(ll j = n; j >= 0; --j){
      for(ll i = m; i >= 0; --i){
        if(dp[j][i] != inf && dp[j][i] != 0 && dp[j][i] <= m){
          ans = i;
          j = -1;
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll MAX_N = ll(1e5 + 5);
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i,n) for (int i = n; i >=0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()


int main() {
    int n, m, ans, tmp;
    vector<int> p, sum;
    while(1){
        cin >> n >> m;
        if(n==0)break;
        p.resize(n);
        sum.clear();
        REP(i,n)cin >> p[i];
        SORT(p);
        for(int i = 0; i<n; i++){
            if(p[i] > m)break;
            sum.push_back(p[i]);
            if(p[i] > m/2)continue;
            for(int j = i; j<n; j++){
                if(p[i]+p[j]>m)break;
                sum.push_back(p[i]+p[j]);
            }
            
        }
        sum.push_back(0);
        SORT(sum);
        ans = 0;
        for(auto& itr : sum){
            if(itr > m/2)break;
            tmp = itr + *--upper_bound(ALL(sum),m-itr);
            if(tmp > m)continue;
            ans = max(ans, tmp);
            if(ans == m)break;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const long long inf = 8000000000000000;
struct P {
	ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b. cost; }
bool operator>(P a, P b) { return a.cost > b. cost; }
int main() {
	ll a[1000], n, m;
	while (cin >> n >> m&&n != 0 && m != 0) {
		for (int i = 0; i < n; i++) cin >> a[i];
		ll b[1000];
		fill(b, b + n, -1);
		b[0] = a[0];
		priority_queue<P, vector<P>, greater<P>>p;
		p.push(P{ 0,a[0] });
		while (!p.empty()) {
			P t = p.top(); p.pop();
			for (int i = 0; i < n; i++) {
				if (a[i] + b[t.pos] > b[i] && a[i] + b[t.pos] <= m) {
					b[i] = a[i] + b[t.pos];
					p.push(P{ i,b[i] });
				}
			}
		}
		cout << max(b[n - 1],(ll)0) << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
#include <cassert>
using namespace std;

//common
#define BR "\n"

#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))

typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;

//config
#define MODE_DEBUG
//#define INF 1<<30
//#define EPS 1e-8
//const ll MOD =100000007;

//debug
#ifdef MODE_DEBUG
#define  DUMP(x)  cerr << #x << " = " << (x) <<endl
#define DEBUG(x) DUMP(x) << " (L" << __LINE__ << ")" << " " << __FILE__<<endl
#define CHECK(exp,act)  if(exp!=act){DUMP(exp);DEBUG(act);}
#define STOP(e)  CHECK(e,true);if(!(e)) exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp,act)
#define STOP(e)
#endif

template<class T> inline string toString(const vector<T>& x) {
	stringstream ss;
	REP(i,x.size()){
		if(i!=0)ss<<" ";
		ss<< x[i];
	}
	return ss.str();
}
template<class T> inline string toString(const vector<vector<T>>& map) {
	stringstream ss;
	REP(i,map.size()){
		if(i!=0)ss<<BR;
		ss<< toString(map[i]);
	}
	return ss.str();
}
template<class K,class V>  string toString(map<K,V>& x) {
	string res;stringstream ss;
	for(auto& p:x)ss<< p.first<<":" << p.second<<" ";
	return ss.str();
}

template<typename T,typename V> inline T mod(T v,V MOD){
	return (v%MOD+MOD)%MOD;
}


int main(){
	while(true){
		int N;ll M;cin >> N >> M;
		if(N==0 && M ==0)break;
		vector<ll> ps(N+1);//0 
		REP(i,N)cin >>ps[i];
		sort(ALL(ps));
		vector<ll> ps2((N+1)*(N+1));
		REP(i,(N+1))REP(j,(N+1))ps2[i*(N+1)+j]=ps[i]+ps[j];
			
		ll Mv=0;
		for(int i=0;i<(N+1)*(N+1);i++){
			int v1=ps2[i];

			int l=0,r=(N+1)*(N+1);
			while(r-l>1){
				int m=(l+r)/2;
				if(v1+ps2[m]<=M){
					Mv=max(Mv,v1+ps2[m]);
					l=m;
				}else{
					r=m;
				}
			}
		}
		cout << Mv<<endl;
	}
	return 0;
}