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 <bits/stdc++.h>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    cums(T& v){ T r; r.push_back(v[0]); for(int i=1; i<v.size(); i++) r.push_back(r[i-1] + v[i]); return r; }
TEMP_T void maxe(T& v, T m){ v = max(v, m); }
TEMP_T void mine(T& v, T m){ v = min(v, m); }

static inline int in(){ int x; scanf("%d", &x); return x; }

int main()
{
  int n, m;
  while (n = in(), m = in()){
    vector<int> p;
    for (int i = 0; i < n; i++) p.push_back(in());
    p.push_back(0);
    n++;
    vector<int> pp;
    for (int i = 0; i < n; i++){
      for (int j = 0; j < n; j++){
        pp.push_back(p[i] + p[j]);
      }
    }
    int maxi = 0;
    sort(pp);
    for (int i = 0; i < pp.size(); i++){
      int v = pp[i] + upper_bound(pp.begin(), pp.end(), m - pp[i])[-1];
      if (v <= m) maxi = max(maxi, v);
    }
    printf("%d\n", maxi);
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;

int dp[4][200000001];
int main(){
    while(true){
	int n,m;
	cin >> n >> m;
	if(n == 0 && m == 0) break;
	cin >> n >> m;
	if(n == 0 && m == 0) break;
    }
    return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

int N, M, arr[1005];

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    while (true) {
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        arr[0] = 0; ++N;
        for (int j = 1; j < N; ++j) {
            cin >> arr[j];
        }
        vector<int> arr2;
        for (int j = 0; j < N; ++j) for (int k = 0; k < N; ++k) if (arr[j] + arr[k] <= M) {
            arr2.push_back(arr[j] + arr[k]);
        }
        sort(begin(arr2), end(arr2));
        const int NN = arr2.size();

        int ans = 0, r = NN-1;
        for (int j = 0; j < NN; ++j) {
            while (arr2[j] + arr2[r] > M) --r;
            if (arr2[j] + arr2[r] <= M) {
                ans = max(ans, arr2[j] + arr2[r]);
            }
        }

        cout << ans << endl;
    }
    return 0;
}

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

#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repm(a,b,c) for(int a=(b-1);a>=c;a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d",&a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s){double ans=0;rep(i,0,s.length()){ans+=(s[i]-'0')*pow(10,s.length()-i-1);}return (int)ans;}
inline string numstr(int m){str s="";while(m>0){char c;int a=m/10;if(a>0)a=m%(a*10);else a=m;c=(char)('0'+a);s+=c;m/=10;}str st="";for(int i=s.size()-1;i>=0;i--){st+=s[i];}return st;}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
vi cost;
int d[1010];
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,k;
    while(1){
    cost.clear();
    cin >> n >> k;
    if(n==0&&k==0)break;
    rep(i,0,n){
        cin >> d[i];
    }
    d[n]=0;
    int cou=0;
    rep(i,0,n+1){
        rep(j,0,n+1){
            int u=d[i]+d[j];
            cost.pb(u);
        }
    }
    sort(ALL(cost));
    int ans=0;
    for(int v : cost){
        //    cout << v << endl;
        int an=k-v;
        if(an<0)break;
        int smal=0,larg=cost.size()-1;
        while(1){
            int cen=(smal+larg)/2;
            if(cost[cen]>an){
                larg=cen;
            }else if(cost[cen]<an){
                smal=cen+1;
            }else{
                ans=k;
                break;
            }
            if(larg<=smal){
                int a=cost[larg-1];
                if(ans<(v+a))ans=v+a;
                break;
            }
        }
    }
    cout << ans << endl;
    }
    return 0;
}

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

#define INF 100000000
int main(){
    int n, m;
    while(cin >> n >> m, n||m){
        vector<int> v;
        vector<int> sum;
        for(int i=0; i<n; ++i){
            int a; cin >> a;
            v.push_back(a);
            sum.push_back(a);
        }
        
        for(int i=0; i<n; ++i){
            for(int j=0; j<n; ++j){
                sum.push_back(v[i]+v[j]);
                //cout << sum[i*n+j] << end;
            }
        }
        sort(sum.begin(), sum.end());
        int ans=0;
        for(int i=0; i<sum.size(); i++){
            
            int a = sum[lower_bound(sum.begin(), sum.end(), m-sum[i])-sum.begin()-1];
            if(a+sum[i]==m){
                ans = m;
            }else if(sum[i]+a<=m){
                ans = max(ans, sum[i]+a);
            }
        }
        cout << ans << endl;
        v.clear(); sum.clear();
    }
}

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

int main(void) {

    unsigned int N, M;
    while (cin >> N >> M, N || M) {
        vector<unsigned int> P(N+1);
        P[0] = 0;
        for (int i = 1; i <= N; i++) cin >> P[i];

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

        unsigned int ans = 0;
        for (int i = 0; i < P2.size(); i++) {
            if (P2[i] > M) break;
            unsigned int left = 0, right = P2.size()-1;
            unsigned int mid;
            for (int j = 0; j < 100; j++) {
                mid = (left+right)/2;
                if (P2[i]+P2[mid] > M) {
                    right = mid;
                } else {
                    left = mid;
                }
            }
            ans = max(ans, P2[i]+P2[mid]);
        }
        cout << ans << endl;
    }

    return 0;
}