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;
int main(){
  int n,m,p[1001],ans,yoso;
  while(cin >> n >> m){
    if(n == 0 && m == 0) break;
    vector<int> vec;
    for(int i=0;i<n;i++) cin >> p[i];
    p[n] = 0;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
	vec.push_back(p[i]+p[j]);
      }
    }
    sort(vec.begin(),vec.end());
    ans = 0;
    for(int i=0;i<=vec.size();i++){
      yoso = upper_bound(vec.begin(),vec.end(),m-vec[i])-vec.begin()-1;
      if(yoso >= 0) ans = max(ans,vec[i] + vec[yoso]);
    }
    cout << ans << endl;
  }
  return (0);
}

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[5][1010];

int main(){
	while (cin >> n >> m&&n + m){
		fill(p, p + 1010, -1);
		p[0] = 0;
		FOR(i, 1, n + 1)cin >> p[i];
		sort(p, p + 1010, greater<int>());
		int ans = 0;
		REP(i, 4){
			REP(j, n + 1){
				if (ans + p[j] <= m){
					ans += p[j];
					break;
				}
			}
		}
		cout << ans << endl;
	}
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
//http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
#include<cstring>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define P pair<int,int>
#define ll __int64

int n,m;
int t[10000];
const int MA=1000000;
int s1[MA],s2[MA];

int ya[4];

int main(){

while(cin>>n>>m,n||m){
//rep(i,MA)s1[i]=s2[i]=0;
rep(i,10000)t[i]=0;
for(int i=1;i<=n;i++)cin>>t[i];
for(int i=0;i<=n;i++){
	for(int j=0;j<=n;j++){
		s1[i*(n+1)+j]=t[i]+t[j];
		s2[i*(n+1)+j]=t[i]+t[j];
	}
}
int g=(n+1)*(n+1);

sort(s1,s1+g);
sort(s2,s2+g);

//rep(i,g)cout<<s1[i]<<" ";cout<<endl;

int ans=0;

for(int i=0;i<g;i++){
	int now=s1[i];

	int l=0;
	int r=g-1;
	int mid=(l+r)/2;
	int x=s2[mid];

	for(int i=0;i<100;i++){
		if(x+now==m)break;
		if(x+now>m){
			r=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
		else{
			l=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
	}

	//cout<<" "<<x<<" "<<now<<" "<<x+now<<endl;
	if(x+now<=m)
	ans=max(ans,x+now);
}
cout<<ans<<endl;
}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(true){
		cin>>n>>m;
		if(n==0)	return 0;
		vector<int> p(n);
		for(int i=0;i<n;i++)	cin>>p[i];
		p.push_back(0);
		set<int> s;
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(auto it=s.begin();it!=s.end();it++){
			if(*it>=m)	break;
			auto it2=s.lower_bound(m-*it);
			while(it2!=s.begin()&&*it2+*it>=m)	it2--;
			if(*it+*it2<m)	ans=max(ans,*it+*it2);
		}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
/*bool ok(int *q,int m,int h,int count){
  for(int i=0;i<count;i++){
    if(q[i]>m) continue;
    for(int j=0;j<count;j++){
      if(q[i]+q[j]<=m&&q[i]+q[j]>h) return true;
    }
  }
  return false;
  }*/
int main(){
  int n,m;
  while(cin>>n>>m){
    if(n==0&&m==0) return 0;
    int data[n+1];
    int q[(n+1)*(n+1)/2];
    int ans = 0;
    for(int i=0;i<n;i++) cin >> data[i];
    data[n] = 0;
    int count = 0;
    for(int i=0;i<=n;i++){
      for(int j=i;j<=n;j++){
	if(data[i]+data[j]>m) continue; 
	  q[count] = data[i] + data[j];
	  count++;
      }
    }
    for(int i=0;i<count;i++){
      for(int j=0;j<count;j++){
	if(q[i]+q[j]>m) continue;
	ans = max(ans,q[i]+q[j]);
      }
    }
      //sort(q,q+count);
    cout << ans << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
using LL = long long;

int main() {
    int N, M, P[1002];

    while (cin >> N >> M) {
        if ((N | M) == 0) break;

        P[0] = 0;
        for (int i = 1; i <= N; i++) cin >> P[i];

        vector<LL> S;
        for (int i = 0; i <= N; i++) {
            for (int j = 0; j <= N; j++) {
                S.push_back(P[i] + P[j]);
            }
        }
        sort(S.begin(), S.end());

        LL ans = 0;
        for (auto s : S) {
            if (M - s > 0) {
                ans = max(ans, s + *(lower_bound(S.begin(), S.end(), M - s) - 1));
            }
        }

        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long  ll;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    ll m;

    while (cin >> n >> m, n != 0 && m != 0) {
        ++n;
        vector<int> p(n);
        p[0] = 0;
        for (int i = 1; i < n; ++i)
            cin >> p[i];

        vector<int> dp(n * n);
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                dp[i * n + j] = p[i] + p[j];

        sort(dp.begin(), dp.end());
        dp.erase(unique(dp.begin(), dp.end()), dp.end());

        n = dp.size();
        ll ans = 0;
        for (int i = 0; i < n; ++i) {
            if (m < dp[i])
                break;
            ll tmp = *(upper_bound(dp.begin(), dp.end(), m - dp[i]) - 1);
            if (tmp + dp[i] <= m)
                ans = max(ans, tmp + dp[i]);
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int p[1001];
int main(){
	int n,m;
	while(true){
	cin>>n>>m;
	if(n+m==0)break;
	for(int i=0;i<n;i++)cin>>p[i];
	p[n]=0;sort(p,p+n+1);
	int ans=0;
	for(int i=0;i<=n;i++){
		for(int j=0;j<=i;j++){
			for(int k=0;k<=j;k++){
				int key=m-p[i]-p[j]-p[k];
				if(key>=0){
				int left=0,right=k+1,mid,cnt=0;
				while(true){
					if(left>=right-1)break;
					mid=(left+right)/2;
					if(p[mid]<=key){cnt=max(cnt,mid);left=mid;}
					else right=mid;
				}
				ans=max(ans,m-key+p[cnt]);
				}
			}
		}
	}
	cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int sum[1000100];

int main(){
  int n,d;
  int m[1111];
  while(cin >> n >> d , n){
    memset(sum,0,sizeof(sum));
    for(int i=0;i<n;i++){
      cin >> m[i];
    }
    m[n] = 0;
    n++;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	sum[i*n+j] = m[i] + m[j];
      }
    }
    sort(sum,sum+n*n);
    
    int res = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	int ab = m[i] + m[j];
	if(m[i] + m[j] <= d){
	  int h = d - (m[i] + m[j]);
	  int tmp = sum[upper_bound(sum,sum+n*n,h)-sum-1] + ab;
	  if(tmp <= d){
	    res = max(res,tmp);
	  }
	}
      }
    }
    cout << res << endl;
  }
}