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 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(void) {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> va(1, 0);
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      if (a > m) continue;
      va.push_back(a);
    }
    sort(va.begin(), va.end());

    int az = va.size();
    vector<int> vb;
    for (int i = 0; i < az; i++) {
      for (int j = i; j < az; j++) {
        if (va[i] + va[j] > m) break;
        vb.push_back(va[i] + va[j]);
      }
    }
    sort(vb.begin(), vb.end());

    int bz = vb.size(), l = 0, r = 0;
    vector<int> vc;
    while (l < bz) {
      vc.push_back(vb[l]);
      while (r < bz && vb[r] == vb[l]) r++;
      if (vb[r] == vb[l]) break;
      l = r;
    }

    int cz = vc.size(), ans = 0;
    for (int i = 0; i < cz; i++) {
      int p = lower_bound(vc.begin(), vc.end(), m - vc[i]) - vc.begin();
      if (vc[i] + vc[p] <= m) {
        ans = max(ans, vc[i] + vc[p]);
      } else {
        ans = max(ans, vc[i] + vc[p - 1]);
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

int main(){
  long n,m,p[1001];
  while(cin>>n>>m,n||m){
    for(int i=1;i<=n;++i) cin>>p[i];
    p[0]=0;
    vector<long> s(0);
    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());
    s.erase(std::unique(s.begin(), s.end()), s.end());
    long a=0;
    for(vector<long>::iterator it=s.begin();*it<m/2;++it){
      a=max(a,*it+*(upper_bound(s.begin(),s.end(),m-*it)-1));
    }
    cout<<a<<endl;
  }
  return 0;
}

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

int main()
{
	int n,m,in,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&in);
			P.push_back(in);
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			if(P[i] + *it <= m){
				MAX = max(MAX,P[i] + *it);
			}
		}
	
		printf("%d\n",MAX);
	}
	
	return 0;
}

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

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, M, P[1001];

int main(){
  while(scanf("%d%d", &N, &M) && N+M){
    rep(i, N)  scanf("%d", P+i);
    P[N] = 0;
    vi p;
    rep(i, N+1) rep(j, N+1) if(P[i] + P[j] <= M) p.PB(P[i]+P[j]);
    sort(p.begin(), p.end());

    int res = 0;
    rep(i, p.size()){
      int d = M - p[i];
      int l = 0, h = p.size();
      int m;
      while(h - l > 1){
	m = (l+h)/2;
	if(p[m] < d) l = m;
	else if(p[m] > d) h = m;
	else break;
      }
      if(p[m] > d) m--;
      //      printf("%d %d\n", p[m], p[i]);
      res = max(res, p[m] + p[i]);
    }
    printf("%d\n", res);
  }
  return 0;
}

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

#define MAX_N 1001

using namespace std;

class CNum{
public:
	vector<int> v;
};

bool Find(vector<int> &v, int search){
	for(unsigned int i = 0; i < v.size(); i++){
		if( v[i] == search ){
			return true;
		}
	}
	return false;
}

int main(void){
	while( 1 ){
		int n, m;
		int V[MAX_N] = {0,};
		CNum D[4];
		int Max = 0;

		cin >> n >> m;

		if( n == 0 && m == 0 ){
			break;
		}

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

		sort(V, V + n);
		reverse(V, V + n);

		for(int i = 0; i < n; i++){
			for(int j = 0; j < 4; j++){
				if( j == 0 ){
					D[j].v.push_back( V[i] );
				}else if( j > 0 ){
					D[j].v.clear();
					for(unsigned int l = 0; l < D[j - 1].v.size(); l++ ){
						int tmp = V[i] + D[j - 1].v[l];
						if( tmp <= m ){
							int index = -1;
							if( Max < tmp ){
								Max = tmp;
							}
							for(int k = 0; k < n; k++){
								if( tmp + V[k] <= m ){
									index = k;
									break;
								}
							}
							if( index >= 0 && tmp + V[index] >= Max && !Find(D[j].v, tmp) )
								D[j].v.push_back( tmp );
						}
					}
				}
			}
		}

		for(int i = 0; i < 4; i++){
			//cout << " " << j << ":" << '\n';
			for(unsigned int j = 0; j < D[i].v.size(); j++ ){
				//cout << D[j].v[k] << ' ';
				int tmp = D[i].v[j];
				if( tmp <= m && Max < tmp){
					Max = tmp;
				}
			}
			//cout << '\n';
		}
		cout << Max << '\n';
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int n,m;
  while(cin>>n>>m, n+m) {
    int p[n];
    REP(i,n) 
      cin >> p[i];
    vector<int> v(n*n);
    REP(i,n)
      REP(j,n)
      v[i*n + j] = p[i] + p[j];
    v.push_back(0);
    REP(i,n)
      v.push_back(p[i]);
    sort(ALL(v));
    // FOR(it, v)
    //   cout << *it << " ";
    // cout << endl;
    int res = 0; 
    REP(i,v.size()) {
      vector<int>::iterator it = upper_bound(ALL(v), m-v[i]);
      int tmp;
      if (it == v.begin()) {
        break;
      } else {
        it--;
        tmp = v[i] + *it;
      }
      res = max(res, tmp);
    }
    cout << res << endl;
  }
}