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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m;
int p[1001];
int pp[1500000];
bool binary_search(int x) {
    int l=0, r=(n+1)*(n+1);
    while (r-l>=1) {
        int i=(l+r)/2;
        if (pp[i]==x) return true;
        else if (pp[i]<x) l=i+1;
        else r=i;
    }
    return false;
}
bool C(int x) {
    for (int a=0; a<=n; a++) {
        for (int b=0; b<=n; b++) {
            if (binary_search(x-p[a]-p[b])) {
                return true;
            }
        }
    }
    return false;
}
int main() {
    while (scanf("%d%d",&n,&m)) {
        if (!n&&!m) break;
        memset(p, 0, sizeof(p));
        for (int i=0; i<n; i++) scanf("%d",&p[i]);
        sort(p, p+n+1);
        for (int c=0; c<=n; c++) {
            for (int d=0; d<=n; d++) {
                pp[c*(n+1)+d]=p[c]+p[d];
            }
        }
        sort(pp, pp+(n+1)*(n+1));
        for (int i=m; i>=0; i--) {
            if (C(i)) {
                printf("%d\n",i);
                break;
            }
        }
    }
}

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 = 1000000000;
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);
}

ll n,m;
vector<map<ll,bool>> dp(5);

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(cin >> n >> m && n && m){
    vary(ll,s,1001,0);
    rep(i,n){
      cin >> s[i];
    }
    sort(s.begin(),s.begin()+n);
    dp = vector<map<ll,bool>>(5);
    dp[0][0] = true;
    rep(i,4){
      for(auto &&e : dp[i]){
        if(e.fi > m) break;
        if(e.se){
          dp[i+1][e.fi] = true;
          rep(k,n){
            if(e.fi+s[k] <= m)
              dp[i+1][e.fi+s[k]] = true;
          }
        }
      }
      dp[i].clear();
    }
    ll ans = -1;
    for(auto rit = dp[4].rbegin(); rit != dp[4].rend(); ++rit){
      if(rit->se){
        ans = max(ans,rit->fi);
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main(){
  int n, m;
  while(1){
    scanf("%d%d", &n, &m);
    if(n == 0 && m == 0)return 0;
    int res = 0, tmp;
    set<int> points, points2;
    for(int i = 0;i < n;i++){
      scanf("%d", &tmp);
      points.insert(tmp);
    }
    set<int>::iterator it1,it2;
    for(it1 = points.begin();it1 != points.end();it1++){
      for(it2 = points.begin();it2 != points.end();it2++){
	points2.insert(*it1 + *it2);
      }
    }
    for(it1 = points2.begin();it1 != points2.end();it1++){
      it2 = upper_bound(points2.begin(), points2.end(), m - *it1);
      if(it2 == points2.begin())continue;
      it2--;
      res = max(res, *it1 + *it2);
    }
    printf("%d\n",res);
  }
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#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 vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#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 pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while (cin >> n >> m && n && m) {
    vector<ll> v(n+1);
    rep(i,n){
      cin >> v[i];
    }
    v[n] = 0;
    sort(all(v));
    ll ans = 0;
    vector<ll> k(n + n * n);
    rep(i,n){
      rep(j,n+1){
        if(v[i] + v[j] > m) continue ;
        k[i + j * n] = v[i] + v[j];
        ans = max(ans,k[i+j*n]);
      }
    }
    sort(all(k));
    rep(i,n){
      rep(j,n+1){
        auto it = upper_bound(all(k),m - (v[i] + v[j]));
        if(it != k.begin())
          ans = max(ans,*(it-1) + v[i]+v[j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> a;
vector <int> all;
int n,m;



int bainary_search(int i) {
  int L=0;int R = all.size()-1,M;
  
  while(L<R) {
    M = (L+R)/2;
    if(all[i]+all[M] > m) R = M;
    else L = M+1;
  }
  L--;
  return L;
}

int main() {

  cin >> n >> m;

  a.resize(n);
  for(int i=0;i<n;i++) cin >>a[i];

  a.push_back(0);

  for(int i=0;i<a.size();i++) 
    for(int j=i;j<a.size();j++)
      all.push_back(a[i]+a[j]);

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

  int ans = 0;
  for(int i=0;i<all.size();i++) {
    // cout << all[i] <<endl;
  int res = all[i] + all[bainary_search(i)];
  // cout << res <<endl;
  if(ans < res && res<= m) ans = res;
  }

  cout << ans <<endl;

  return 0;
  
}