Go: ///
// File:  0529.go
// Author: ymiyamoto
//
// Created on Wed Feb 28 20:39:28 2018
//
package main

import (
	"fmt"
	"sort"
)

func max(n, m int) int {
	if n < m {
		return m
	} else {
		return n
	}
}

func main() {
	for {
		var N, M int
		fmt.Scan(&N, &M)
		if N == 0 && M == 0 {
			break
		}

		arr := make([]int, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Ints(arr)

		sums := make([]int, 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.Ints(sums)

		m := 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<vector>
using namespace std;
typedef unsigned int uint;
uint maxv;
void solve(uint now,uint m,uint *a,int n){
  int r=n-1,l=0,mid;
  while(l <=r){
    mid = (l+r)/2;
    if ( a[mid]+now>m)r=mid-1;
    else if ( a[mid]+now <m)l=mid+1;
    else break;
  }
  if ( now+a[mid]>maxv&& now+a[mid]<=m)maxv=now+a[mid];
}


main(){
  int n;
  uint m;
  while(scanf("%d%u",&n,&m) && n){
    uint one[n];
    uint two[n*n];
    int p=0;
    maxv=0;
    for(int i=0;i<n;i++)scanf("%u",&one[i]);
    for(int i=0;i<n;i++){
      if ( one[i]>maxv &&one[i] <=m)maxv=one[i];
      for(int j=i;j<n;j++){
	if ( one[i]+one[j]>m)continue;
	two[p++]=one[i]+one[j];
	if (two[p-1]>maxv)maxv=two[p-1];
      }
    }
    sort(one,one+n);
    sort(two,two+p);
    for(int i=0;i<p;i++){
      if ( two[i]+one[0]>m)break;
      if ( two[i]+two[0]<=m)solve(two[i],m,two,p);
      if ( two[i]+one[0]<=m)solve(two[i],m,one,n);
    }
    if ( maxv>m)return true;
    printf("%u\n",maxv);
  }
  return false;
}

C++: #include <iostream>
#include <vector>
#include <set>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() {return val;}
    bool operator!=(Iterator& itr) {return val < itr.val;}
    void operator++() {++val;}
  };
  Iterator i, n;
public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator& begin() {return i;}
  Iterator& end() {return n;}
};

template<class T> inline T at(const vector<T> &v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];}

template<class T> inline bool is_max(T &a, const T &b) {return a < b ? a = b, true : false;}
template<class T> inline bool is_min(T &a, const T &b) {return a > b ? a = b, true : false;}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    vector<int> p;
    for (int _ : range(n)) {
      int x;
      cin >> x;
      p.emplace_back(x);
    }
    p.emplace_back(0);
    set<int> s;
    for (int i : p) for (int j : p) s.insert(i + j);
    int res = 0;
    for (int i : s) {
      auto k = s.upper_bound(m - i);
      if (k == s.begin()) continue;
      --k;
      is_max(res, i + *k);
    }
    cout << res << endl;
  }
}

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

#define MAX_N 1000

int main(){
	int N, M, P[MAX_N + 1];
	int table[(MAX_N + 1) * (MAX_N + 1)];
	int size;
	int ub, lb, mid;
	int res;
	
	while(true){
		scanf("%d%d", &N, &M);
		if(N == 0 && M == 0) break;
		for(int i = 1; i <= N; i++){
			scanf("%d", &P[i]);
		}
		P[0] = 0;
		
		for(int i = 0; i <= N; i++){
			for(int j = 0; j <= N; j++){
				table[(i * (N + 1)) + j] = P[i] + P[j];
			}
		}
		size = (N + 1) * (N + 1);
		sort(table, table + size);
		
		res = 0;
		for(int i = 0; i < size; i++){
			if(table[i] > M) continue;
			ub = size; lb = 0;
			while(ub - lb > 1){
				mid = (ub + lb) / 2;
				if(table[i] + table[mid] <= M){
					lb = mid;
				}
				else{
					ub = mid;
				}
			}
			if(res < table[i] + table[lb]){
				res = table[i] + table[lb];
			}
		}
		
		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 cand = 0;
							int index = -1;

							if( Max < tmp ){
								Max = tmp;
							}
							for(int k = 0; k < n; k++){
								cand = tmp + V[k];
								if( cand <= m ){
									index = k;
									break;
								}
							}
							if( index >= 0 && cand >= Max && !Find(D[j].v, tmp) ){
								Max = cand;
								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++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(int i=a; i<int(b); i++)
#define rep(i,b) loop(i,0,b)
#define each(e,c) for(auto& e:c)
#define iter(it,c) for(auto it=c.begin(); it!=c.end(); ++it)

ll const inf = 1<<28;
double const pi = acos(-1);

int a[1024];
int d[2048];

int solve(int n, int k){
    int c=0;
    rep(i,n)loop(j,i,n){
        int s=a[i]+a[j];
        if(s<=k){
            d[c++]=s;
        }
    }
    sort(d,d+c);
    int ans=0;

    rep(i,c){
        int s1=d[i];
        int lo=0, hi=c;
        while(lo+1<hi){
            int mid=(lo+hi)>>1;
            int s2=d[mid];
            if(s1+s2>k){
                hi=mid;
            }else{
                lo=mid;
            }
        }
        int s2=d[lo];
        ans=max(ans, s1+s2);
    }
    return ans;
}

int main(){
    int n,k;
    while(cin>>n>>k && n|k){
        a[0]=0;
        rep(i,n)cin>>a[i+1];
        n++;
        cout << solve(n,k) << endl;
    }
}

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

int main() {
	while(true){
	int N,M;
	int num[1001];
	vector<int> nums;
	cin >> N >> M;
	if(N==0)
	break;
	num[0]=0;
	for(int i=1;i<=N;i++){
		cin >> num[i];
	}
	for(int i=0;i<=N;i++){
		for(int j=0;j<=N;j++){
			if(i>=j){
			nums.push_back(num[i]+num[j]);
			
			}
		}
	}
	sort(nums.begin(),nums.end());
	int now=nums.size()-1,maxer=0;
	for(int i=0;i<nums.size();i++){
		if(nums[i]<=M){
	    while(nums[now]+nums[i]>M && now!=0)
		now--;
		
		maxer=max(nums[now]+nums[i],maxer);
		}
	}
	cout << maxer << endl;
	}
	return 0;
}