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 <bits/stdc++.h>
using namespace std;
 
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;
 
#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
 
#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}
 
 
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
 
int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
 
  LL N, M;
while(
  cin >> N >> M,N){
  VL xs(N);
  REP(i,N) cin >> xs[i];
  ++N;
  xs.PB(0);
 
  VL cand;
  REP(i,N) REP(j,N){
	cand.PB(xs[i] + xs[j]);
  }
  SORT(cand);
 
  LL ans = 0;
  for(LL x: cand){
	if(x > M) continue;
	auto it = upper_bound(ALL(cand), M-x);
	--it;
	maxi(ans, x + *it);
  }
  cout << ans << endl;
 }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        int N, M;
        cin >> N >> M;
        if (N == 0 && M == 0) return 0;
        vector<int> a(N);
        rep(i, N) cin >> a[i];
        a.push_back(0);
        N++;
        vector<int> aa(N*N);
        rep(i, N) rep(j, N) aa[i*N + j] = a[i] + a[j];
        sort(aa.begin(), aa.end());

        int ans = 0;
        rep(i, N*N) {
            int tmp = aa[i];
            int aim = M - tmp;
            if(aim < 0) continue;
            tmp += *(upper_bound(aa.begin(), aa.end(), aim) - 1);
            if(tmp <= M ) ans = max(ans, tmp);
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){
      break;
    }
    vector<int> p(n);
    for(int i = 0; i < n; ++i){
      cin >> p[i];
    }
    p.push_back(0);
    vector<int> comb_p;
    for(int i = 0; i < n + 1; ++i){
      for(int j = 0; j < n + 1; ++j){
	if(p[i] + p[j] <= m){
	  comb_p.push_back(p[i] + p[j]);
	}
      }
    }
    sort(comb_p.begin(), comb_p.end());
    comb_p.erase(unique(comb_p.begin(), comb_p.end()), comb_p.end());
    int ans = 0;
    for(int i = 0; i < comb_p.size(); ++i){
      int opp = m - comb_p[i];
      int tmp = comb_p[i] + *(--upper_bound(comb_p.begin(), comb_p.end(), opp));
      ans = max(ans, tmp);
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define INF 100000000
int sum[1001*1000];
int v[1001];
int n, m;

int main(){
    int n, m;
    while(cin >> n >> m, n||m){
        for(int i=0; i<n; ++i){
            int a; cin >> a;
            v[i] = a;
        }
        sort(v, v + n);
        
        int maxV=0;
        
        for(int i=0; i<n; ++i){
            for(int j=0; j<n; ++j){
                sum[i*n+j] = v[i]+v[j];
                //cout << sum[i*n+j] << end;
            }
        }
        sort(sum, sum+n*n);
        for(int i=0; i<n; ++i){
            for(int j=0; j<n; ++j){
                    if(v[i]+v[j]<=m) {
                        maxV=max(maxV,v[i]+v[j]+sum[upper_bound(sum,sum + n*n, m-v[i]-v[j])-sum-1]);
                    }
                }
            
        }
        cout << maxV << endl;
    }
}

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

int main(int argc, char *argv[]){
  int n, m;
  while(cin >> n >> m, n){
    int point[n];
    set<int> a, b;
    for (int i = 0; i < n; i++) {
      cin >> point[i];
    }
    for (int i = -1; i < n; i++) {
      for (int j = -1; j < n; j++) {
        if(i == -1){
          a.insert(point[j]);
        }else if(j == -1){
          a.insert(point[i]);
        }else{
          a.insert(point[i] + point[j]);
        }
      }
    }

    for (int i = -1; i < n; i++) {
      for (int j = -1; j < n; j++) {
        if(i == -1){
          b.insert(point[j]);
        }else if(j == -1){
          b.insert(point[i]);
        }else{
          b.insert(point[i] + point[j]);
        }
      }
    }
    int ans = 0;
    for (int i: a) {
      if(--b.upper_bound(m - i) != b.end()){
        ans = max(ans, i + *(--b.upper_bound(m - i)));
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
vector <int> a;
set <int> all;
int n,m;


int main() {

  while(1) {
    scanf("%d%d",&n,&m);
    if(n == 0 && m ==0) break;
    a.resize(n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);

    a.push_back(0);
    all.clear();

    for(int i=0;i<a.size();i++) 
      for(int j=i;j<a.size();j++)
	all.insert(a[i]+a[j]);

    int ans = 0;
    set <int> :: iterator it,b;
    it = all.end();
    b = all.begin();
    while(1){
      if(it == all.begin()) break;
      it--;
      while(b != all.end()) {
	int sum = *it + *b;
	if(sum > m) break;
	b++;
	if(ans < sum) ans = sum;
      }
      if(ans == m) break;
    }

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

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  int t[n];
  for(int i=0;i<n;i++){
    cin>>t[i];
  }
  int ans=0;
  int cnt=0;
  sort(t,t+n);
  for(int i=n-1;i>=0;){
    if(cnt>3){
      break;
    }
    if(ans+t[i]>m){
      i--;
      continue;
    }
    cnt++;
    ans+=t[i];
    //cout<<cnt<<endl;
    //cout<<"ans "<<ans<<endl;
  }
  cout<<ans<<endl;
  return 0;
}