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<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n,m;
  while(cin>>n>>m,n+m){
    vector<ll>num(n);
    for(int i=0;i<n;i++)cin>>num[i];
    vector<ll>zen;
    vector<ll>kou;
    for(int i=0;i<n;i++){
      for(int j=i;j<n;j++){
	zen.pb(num[i]+num[j]);
      }
      zen.pb(num[i]);
    }
    zen.pb(0);
    sort(zen.begin(),zen.end());
    ll ans=0;
    for(int i=0;i<zen.size();i++){
      ll left=0,right=zen.size()-1;
      ll mid;
      while(left!=right){
	mid=(left+right)/2;
	if(zen[mid]+zen[i]<=m)
	  left=mid+1;
	else if(zen[mid]+zen[i]>m)
	  right=mid;
	else
	  break;
	//	cout<<left<<" "<<right<<endl;
      }
      if(zen[i]+zen[left-1]<=m)
	ans=max(ans,zen[i]+zen[mid-1]);
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main(){
  
  int n, m, p[1111];
  int sum[1111111];

  while(cin >> n >> m, n||m){
    for(int i = 0; i < n; i++) cin >> p[i];
    
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        sum[i * n + j] = p[i] + p[j];
      }
    }
    
    int maxi = 0;
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        if(sum[i * n + j] <= m){
          maxi = max(maxi, sum[i * n + j]);
          if(sum[i * n + j] + p[i] + p[j] <= m)
            maxi = max(maxi, sum[i * n + j] + p[i] + p[j]);
          if(sum[i * n + j] + p[i] <= m)
            maxi = max(maxi, sum[i * n + j] + p[i]);
          if(sum[i * n + j] + p[j] <= m)
            maxi = max(maxi, sum[i * n + j] + p[j]);
        }
      }
    }
    cout << maxi << endl;
  }
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
int n, m, c[1009], s[510009], t[510009], cnt[1027], sum[1027];
int main() {
	while (scanf("%d%d", &n, &m), n) {
		for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
		sort(c, c + n);
		int b = 0;
		for (int i = 0; i <= n; i++) {
			for (int j = i; j <= n && c[i] + c[j] <= m; j++) {
				s[b++] = c[i] + c[j];
			}
		}
		for (int i = 0; i < 30; i += 10) {
			fill(cnt, cnt + 1024, 0); sum[0] = 0;
			for (int j = 0; j < b; j++) cnt[(s[j] >> i) & 1023]++;
			for (int j = 0; j < 1024; j++) sum[j + 1] = sum[j] + cnt[j];
			for (int j = 0; j < b; j++) t[sum[(s[j] >> i) & 1023]++] = s[j];
			for (int j = 0; j < b; j++) s[j] = t[j];
		}
		int r = b - 1, ret = 0;
		for (int i = 0; i < b && i <= r; i++) {
			while (r > 0 && s[i] + s[r] > m) r--;
			ret = max(ret, s[i] + s[r]);
		}
		printf("%d\n", ret);
	}
	return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
  
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
  
using namespace std;
  
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
  
const int INF=1<<29;
const double EPS=1e-9;
  
const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
    while(1) {
        int N, T;
        cin >> N >> T;
        if (N == 0 && T == 0) break;
        vector<int> P(N);
        for (int i = 0; i < N; i++) {
            cin >> P[i];
        }

        vector<int> PP;
        PP.push_back(0);
        for (int i = 0; i < N; i++) {
            PP.push_back(P[i]);
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                PP.push_back(P[i] + P[j]);
            }
        }

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

        int ans = 0;
        for (int i = 0; i < PP.size(); i++) {
            int k = upper_bound(PP.begin(), PP.end(), T - PP[i]) - PP.begin();
            if (k != 0) {
                ans = max(ans, PP[i] + PP[k - 1]);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
	while(1){
	int n,m,p,q=0,r=0,l=0,s=0,e=0,ans=0;
	cin>>n>>m;
		if(n==0 && m==0)
			break;
	vector<int> a,x;
	a.push_back(0);
	for(int i=0;i<n;i++){
		cin>>p;
		a.push_back(p);
	}
	q=n+1;
	//cout<<q<<endl;
	for(int i=0;i<q;i++){
		for(int j=0;j<q;j++){
			x.push_back(a[i]+a[j]);
		}
	}
	sort(x.begin(),x.end());
	//for(int i=0;i<q*q;i++)
	//cout<<x[i]<<' ';
	//cout<<endl;
	for(int i=0;i<x.size();i++){
		r=m-x[i];
		//cout<<r<<"を探します"<<endl;
		s=0;
		e=q*q;
		while(r>=0){
			l=(s+e)/2;
			//cout<<"l="<<l<<endl;
			if(x[l]>r){
				e=l-1;
			}else if(x[l]<r){
				s=l+1;
			}
			if(s>=e || x[l]==r || l==0){
				if(x[i]+x[l]<=m)
				ans=max(ans,x[i]+x[l]);
				break;
			}
		}
		while(1){
			if(l+1>q*q)break;
			if(x[l+1]<=r){
				l++;
				ans=max(ans,x[i]+x[l]);
			}
			else break;
		}
	}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;


signed main() {
	
	while(1) {
		cin2(n,m);
		if (n + m == 0) break;

		vi p(n,0);
		vi v;
		rep(i,0,n) {
			cin1(p[i]);
			v.pb(p[i]);
		}

		rep(i,0,n) {
			rep(j,0,n) {
				if (p[i] + p[j] <= m) { 
					v.pb(p[i] + p[j]);	
				}
			}
		}

		int size = v.size();

		int ans = 0;
		rep(i,0,size) {
			int c_d = v[i];
			int a_b = *(upper_bound(v.begin(),v.end(),m - c_d) - 1);
			ans = max(ans,a_b + c_d);
		}
		cout(ans);
	}



	return 0;
}