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 <iostream>

#include <algorithm>



constexpr int MAX_N=1001;



int n,m;

int P[MAX_N];



int Data[MAX_N*MAX_N];



int main() {
    while(1){
    std::cin>>n>>m;
    if(n+m==0)
    	break;

    for(int i=1;i<=n;++i)

        std::cin>>P[i];

    for(int i=0;i<=n;++i){

		for(int j=0;j<=n;++j)

	    	Data[i*(n+1)+j]=-(P[i]+P[j]);

    }

    std::sort(Data,Data+(n+1)*(n+1));

    int ans=0;

    for(int i=0;i<=n;++i){

		for(int j=0;j<=n;++j){

	   		int x=P[i]+P[j]-m;

	    	int* v=std::lower_bound(Data,Data+(n+1)*(n+1),x);

	    	if(v!=Data+(n+1)*(n+1))

				ans=std::max(ans,P[i]+P[j]-(*v));

		}

    }

    std::cout<<ans<<std::endl;
    }

    return 0;

}

C++: #include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second

typedef pair<int, int> P;

map<int, bool> f;
priority_queue<P> q;
int a[1010];

int main()
{
  int n, m;
  while (cin >> n >> m, n){

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

    sort(a, a + n);
    
    for (int i = n - 1; i >= 0; i--){
      q.push(pair<int, int>(a[i], 1));
    }

    f.clear();
    int maxs = 0;
    while (!q.empty()){
      P s = q.top(); q.pop();

      maxs = max(s.fi, maxs);

      if (s.second == 4) continue;
      for (int i = n - 1; i >= 0; i--){
        if (s.fi + a[i] > m) continue;
        if (f[s.fi + a[i]]) continue;

        q.push(P(s.fi + a[i], s.se + 1));
        f[s.fi + a[i]] = true;
      }
    }

    cout << maxs << endl;
  }
}

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

int N,M,P[1001];
set<int> s;

int main(){
	while(scanf("%d%d",&N,&M), N != 0){
		s = set<int>();
		s.insert(0);
		for(int i = 0; i < N; i++){
			scanf("%d",&P[i]);
			s.insert(P[i]);
		}
		
		for(int i = 0; i < N; i++){
			for(int j = i; j < N; j++){
				s.insert(P[i] + P[j]);
			}
		}

		int mx = 0;
		
		for(set<int>::iterator itr = s.begin(); itr != s.end(); itr++){
			set<int>::iterator tmp = lower_bound(s.begin(),s.end(),M-*itr);
			if(*itr + *tmp > M){
				if(tmp != s.begin()){
					tmp--;
				}
				else{
					continue;
				}
			}
			if(mx < *itr + *tmp){
				mx = *itr + *tmp;
			}
		}

		printf("%d\n",mx);
	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main(void){
  int n, m;
  int p[1024];
  vector<long long> v;
  for( ; ; ){
    scanf("%d %d", &n, &m);
    if(!(n + m)) break;
    for(int i = 0;i < n; i++) scanf("%d", p + i);
    v.clear();
    p[n++] = 0;
    for(int i = 0;i < n; i++)
      for(int j = i;j < n; j++)
	v.push_back(p[i] + p[j]);
    sort(v.begin(), v.end());
    long long ans = 0;
    for(int i = 0;i < v.size(); i++){
      if(v[i] > m) break;
      vector<long long>::iterator it = upper_bound(v.begin(), v.end(), m - v[i]);
      ans = max(ans, v[i] + *(it - 1));
    }
    printf("%d\n", ans);
  }
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <algorithm>
#include <array>
#include <numeric>
using namespace std;

const auto gcu = getchar_unlocked;
const auto pcu = putchar_unlocked;
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _sc _il bool scan
_T T in(int c){T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_il int in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _ot _il void out
#define _ol _il void outl
_ot(bool b) {pcu('0'+b);}
_ot(const char *s){while(*s)pcu(*s++);}
_ot(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_ot(string s){for(char c:s)pcu(c);}
#endif
_T _ot(T n){static char b[20];char *p=b,m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=n%10*m+'0',n/=10;while(p!=b)pcu(*--p);}
_ol(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _ot(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _ot(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _ol(T&&... t){out(move(t)...);outl();}

void radix_sort(vector<int>::iterator b,vector<int>::iterator e) {
    vector<int> t(e - b);
    array<int, 256> c, s;
    for (int i = 0; i < 32; i += 8) {
        fill(c.begin(), c.end(), 0);
        for_each(b, e, [&](int j) {c[(j >> i) & 255]++;});
        s[0] = 0;
        partial_sum(c.begin(), c.end() - 1, s.begin() + 1);
        for_each(b, e, [&](int j) {t[s[(j >> i) & 255]++] = j;});
        copy(t.begin(), t.end(), b);
    }
}

int main() {
	for (int N, M; N = in(), M = in();) {
		vector<int> t(N), u;
		for (int &p: t)
			p = in();
		t.push_back(0);
		for (auto i = t.begin(); i != t.end(); i++)
			for (auto j = i; j != t.end(); j++)
				if (*i + *j <= M)
					u.push_back(*i + *j);
		radix_sort(u.begin(), u.end());
		int r = 0;
		for (auto i = u.begin(), e = u.end() - 1; i <= e; i++) {
			for (; *i + *e >= M; e--)
				;
			r = max(r, *i + *e);
		}
		outl(r);
	}
}

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

int n, m, ans;
int P[MAX_N+1];
vector<int> Q;

int BinarySearch(int q){
  int left = 0, right = Q.size();
  while(right - left > 1){
    int mid = (left + right) / 2;
    if(Q[mid] <= q) left = mid;
    else right = mid;
  }
  return left;
}

int main(void){
  while(cin >> n >> m && n != 0 && m != 0){
    P[0] = 0;
    for(int i = 1; i <= n; i++) cin >> P[i];
    Q.clear();
    for(int i = 0; i <= n; i++) for(int j = 0; j <= n; j++){
      int q = P[i] + P[j];
      Q.push_back(q);
    }
    sort(Q.begin(), Q.end());
    ans = 0;
    for(int i = 0; i < Q.size(); i++){
      if(Q[i] > m) break;
      int j = BinarySearch(m - Q[i]);
      int s = Q[i] + Q[j];
      if(s > ans) ans = s;
    }
    cout << ans << endl;
  }
  return 0;
}