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 <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
	while(1){
		llong n, m;
		scanf("%lld%lld", &n, &m);
		if(n==0) return 0;

		vector<llong> v(n);
		REP(i, n){
			scanf("%lld", &v[i]);
		}
		v.push_back(0);
		n++;

		vector<llong> a;
		REP(i, n){
			REP(j, n){
				a.push_back(v[i]+v[j]);
			}
		}
		VSORT(a);

		llong ans=0;
		REP(i, n){
			REP(j, n){
				llong tmp=v[i]+v[j];
				llong k=upper_bound(a.begin(), a.end(), m-tmp)-a.begin();
				if(k==a.size()) continue;
				k--;

				if(k>=0) ans=max(ans, tmp+a[k]);
			}
		}

		printf("%lld\n", ans);
	}
}

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

long long int solve(long long int a, vector<long long int>& side_b, long long int m){
  int left = 0, right = side_b.size();
  while(right - left > 1){
    int middle = (right + left) / 2;
    if(side_b[middle] + a <= m){ left = middle; }
    else{ right = middle; }
  }
  return a + side_b[left];
}


int main(void){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){ break; }

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

    vector<long long int> side_a;
    vector<long long int> side_b;
    for(int i = 0; i < n; i++){
      side_a.push_back(input[i]);
      side_b.push_back(input[i]);
      for(int j = 0; j < n; j++){
        side_a.push_back(input[i] + input[j]);
        side_b.push_back(input[i] + input[j]);
      }
    }
    sort(side_a.begin(), side_a.end());
    sort(side_b.begin(), side_b.end());

    long long int ans = 0;
    for(size_t i = 0; i < side_a.size(); i++){
      long long int a = side_a[i];
      long long int tmp = solve(a, side_b, m);
      if(tmp <= m){ ans = max(ans, tmp); }
    }
    
    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

int n, m, in[1001], data[1002001];

main(){
  while(scanf("%d %d", &n, &m) && (n|m)){
    in[0] = 0;
    for(int i=1;i<=n;i++) scanf("%d", &in[i]);
    for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) data[i*(n+1)+j] = in[i] + in[j];
    int size = (n+1)*(n+1), ans = 0;
    sort(data, data+size);
    for(int i=0;i<size;i++){
      int l = 0, r = size-1;
      while(r-l>1){
        int med = (l+r) / 2;
        if(data[i] + data[med] <= m) l = med;
        else r = med;
      }
      if(data[i] + data[l] <= m) ans = max(ans, data[i] + data[l]);
    }
    printf("%d\n", ans);
  }
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <memory>
#include <memory.h>
#include <fstream>
#include <cmath>
#include <math.h>
#include <numeric>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <map>
#include <iomanip>
#include <list>
#include <cctype>
#include <algorithm>
#include <complex>

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 28;
const double EPS = 1E-10;

#define rep(i, n) for(int i = 0; i < n; i++)

vector<int> vi;
int nums[1000];

int main(){
	int n, m;
	while(cin >> n >> m && (n || m)){
		int res = 0;
		vi.clear();
		rep(i, n)cin >> nums[i];
		nums[n] = 0;
		rep(i, n + 1)rep(j, n + 1) if(nums[i] + nums[j] <= m)vi.push_back(nums[i] + nums[j]);
		sort(vi.begin(), vi.end());
		int sz = vi.size();
		rep(i, sz){
			int lb = 0;
			int ub = sz;
			while(ub - lb > 1){
				int mb = (lb + ub) / 2;
				if(vi[i] + vi[mb] <= m) lb = mb;
				else ub = mb;
			}
			res = max(res, vi[i] + vi[lb]);
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
vector<long int> po;
vector <pair <int ,long int> > sss;
long int n,a,low,m,lastans,memo[1005];
void solve(int dp,long int sum);

int main(){
    while(1){
        lastans=0;
        po.clear();
        //for(int i=0;i<1005;i++)memo[i]=0;

        cin >> n >> m;
        if(n==0&&m==0)break;
        for(int i=0;i<n;i++){
            cin >> a;
            po.push_back(a);
           // low=min(low,a);
        }
        sort(po.begin(),po.end());
        //cout << po[0] << endl;
        //solve(0,0);
        //lastans=po[n-1];

        for(int i=4;i>0;i--){
            for(int j=n-1;j>=0;j--){
                if(m>=i*po[j]){
                    lastans=max(i*po[j],lastans);

                    for(int k=4-i;i>0;i--){
                        for(int s=0;s<n;s++){
                            if(m>=i*po[j]+po[s]){
                                lastans=max(i*po[j]+po[s],lastans);

                                for(int t=4-k;t>0;t--){
                                    for(int u=0;u<n;u++){
                                        if(m>=i*po[j]+po[s]+po[u]){
                                         lastans=max(i*po[j]+po[s]+po[u],lastans);

                                         for(int v=4-t;v>0;v--){
                                            for(int w=u-1;w>=0;w--){
                                                if(m>=i*po[j]+po[s]+po[u]+po[w]){
                                                    lastans=max(lastans,i*po[j]+po[s]+po[u]+po[w]);
                                                    break;
                                                }else break;
                                            }
                                         }
                                            break;
                                        }else break;
                                    }
                                }
                                break;
                            }else break;

                        }
                    }

                    //lastans=max(lastans,po[j]*i);
                    //break;
                }else break;
            }
        }



        cout << lastans << endl;
        //cout <<"ss=" << ss << endl;
    }

    return 0;
}