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 <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(k,n){
        rep(j,n){
          if(dp[i][k] + s[j] <= m){
            dp[i+1][j] = max(dp[i+1][j],dp[i][k]+s[j]);
          }
          else{
            dp[i+1][j] = dp[i][j];
          }
        }
      }
    }
//     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<algorithm>
#include<vector>

#define MAX_N 1001

using namespace std;

int Uniq(vector<int> &v, int size){
	int eraseNum = 0;
	for(int i = 0; i < size - eraseNum; i++){
		int sc = v[i];
		for(int j = i + 1; j < size - eraseNum; j++ ){
			if(sc == v[j]){
				v.erase( v.begin() + j );
				eraseNum++;
				j--;
			}else if(sc < v[i]){
				break;
			}
		}
	}
	return eraseNum;
}

int BinSearch(vector<int> &v, int size, int search){
	int l = 0;
	int r = size;
	int m = 0;
	while( r - 1 > l ){
		m = (r + l) / 2;

		if( v[m] > search ){
			r = m;
		}else if(v[m] == search){
			return m;
		}else{
			l = m;
		}
	}
	return m;
}

int main(void){
	while( true ){
		int index = 0;
		int Max = 0;
		int N, M, Qn;
		vector<int> Q(MAX_N*MAX_N,0);
		vector<int> V(MAX_N,0);

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

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

		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				//cout << index++ << '\n';
				Q[i * N + j] = V[i] + V[j];
			}
		}

		//cout << "Sorting..." << endl;
		sort(Q.begin(), Q.begin() + N * N);

		//cout << "Uniqing..." << endl;
		Qn = N * N - Uniq( Q, N * N );

		for(int i = 0; i < Qn; i++){
			//cout << Q[i];
			int optim = BinSearch(Q, Qn, M - Q[i]);
			//cout << ' ' << optim << ":" << Q[optim] << '\n';
			if( optim > 0 && Q[i] + Q[optim] > M) optim--;
			Max = max( Max , Q[i] + Q[optim] > M ? 0 : Q[i] + Q[optim]);
		}

		cout << Max << '\n';
	}
	return 0;
}

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

int main(){

    int N, M, p[1011];

    while(cin >> N >> M, N){
        vector <int> sum;
        p[0] = 0;
        for(int i = 1; i <= N; i++) cin >> p[i];
    
        for(int i = 0; i <= N; i++){
            for(int j  = 0; j <= N; j++){
                sum.push_back(p[i] + p[j]);
            }
        }
    
        sort(sum.begin(), sum.end());
        int res = 0;
        for(int i = 0; i  < sum.size() && sum[i] <= M; i++){
            vector <int>::iterator it = lower_bound(sum.begin(), sum.end(), M - sum[i]);
            res = max(res, sum[i] + *(it - 1));
        }
        cout << res << endl;
    }
}

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

int main(){
while(1){
	int n,m;
	vector<int> p,q;
	scanf("%d%d",&n,&m);
if(n==0&&m==0)break;
	q.push_back(0);
	for(int i=0;i<n;i++){
		int a;
		scanf("%d",&a);
		p.push_back(a);
		q.push_back(a);
	}
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			q.push_back(p[i]+p[j]);
		}
	}
	sort(q.begin(),q.end());
	int ans=0;
	int f=0,s=q.size()-1;
	while(1){
		if(q[f]+q[s]>ans&&q[f]+q[s]<=m)ans=q[f]+q[s];
		if(q[f]+q[s]<=m)f++;
		if(q[f]+q[s]>m)s--;
		if(f>s)break;
	}
	printf("%d\n",ans);
}
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <stack>
#include <queue>
  
using namespace std;
  
#define pb(n)   push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi  first
#define se  second
#define all(r) (r).begin(),(r).end()
  
#define REP(i,s,e)  for(int i=(s); i<(e); i++)
#define rep(i,n)    REP(i,0,n)
#define REPE(i,s,e) for(int i=s; i>e; i--)
#define repe(i,n)   REPE(i,n,-1)
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
  
typedef vector<vi> vvi;
  
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007
 
 
 
const int MAX_N = 1000;
const int MAX_W = 200000000;
 
// int dp[MAX_N + 1][MAX_W + 1][5];
// int v[MAX_N], w[MAX_N];
// int N,W;
 
 
// int solve() {
//     for(int i = 0; i < N; i++) {
//         for(int j = 0; j <= W; j++){
//             for(int k = 0; k < 5; k++){
//                 if(j < w[i] || k <1) {
//                     dp[i + 1][j][k] = dp[i][j][k];
//                 }
//                 else{
//                     dp[i + 1][j][k] = max(/*max(*/dp[i][j][k]/*,dp[i][j-w[i]][k-1] + v[i])*/, dp[i + 1][j - w[i]][k-1] + v[i]);
//                 }
//             }
//         }
//     }
//     return dp[N][W][4];
// }

int dp[MAX_W + 1][5];

int solve(){
	for(int i = 0; i < N; i++){
		for(int j = w[i]; j <= W; j++){
			for(int k = 0; k < 5; k++){
				if(k>=1)dp[j][k] = max(dp[j][k], dp[j-w[i]][k-1] + v[i]);
			}
		}
	}
}
 
int main(){
    int n, m;
    while(cin>>n>>m){
        N = n;
        W = m;
        for(int i = 0; i < n; i++){
            cin >> v[i];
            w[i] = v[i];
        }
        cout<<solve()<<endl;
    }
}