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 <algorithm>
#include <functional>
#include <cstring>
#include <cstdlib>

using namespace std;

int array_sum( int ary[4][1001], int idx, int size )
{
	int sum = 0;
	for(int i=0; i < size; ++i) sum += ary[i][idx];
	return sum;
}
void map_sort( int ary[4][1001], int idx, int size )
{
	for(int i=0; i < size; ++i)
		for(int j=i+1; j < size; ++j) {
			if(ary[i][idx] < ary[j][idx])
				swap(ary[i][idx], ary[j][idx]);
		}
}

int main( void )
{
	int n=0, m, input[1001], map[4][1001];
	while(cin >> n >> m, n | m) {
		for(int i=0; i < n; ++i) {
			cin >> input[i];
			for(int j=0; j < 4; ++j) map[j][i] = 0;
		}
		for(int i=0; i < 4; ++i) map[i][n] = 0;
		sort(input, input+n, greater<int>());
		int sum = 0, count = 0;
		while(sum < m) {
			sum += input[0];
			++count;
		}
		int mmm = m, cc = 0;
		for(int i=0; i < n; ++i) {
			while(input[i] <= mmm && cc < 4) {
				mmm -= input[i];
				++cc;
			}
			if(cc >= 4) break;
		}
		sort(input+1, input+n);
		for(int i=0; i < count; ++i) map[i][0] = input[0];
		int sub = sum - m;
		if(!sub) { cout << m << endl; continue; }
		else if(sub < 0) { cout << sum << endl; continue; }
		int Min = min(m, mmm);
		//cout << Min << endl;
		input[n] = 0;
		for(int i=0; i < n; ++i) {
			//cout << input[i] << endl;
			sum = array_sum(map, i, count);
			if(sum == m) { Min = 0; break; }
			int next = i + 1, ii = i + 1;
			for(int j=0; j < count; ++j) {
				int tsum = sum - map[j][i] + input[ii];
				int tsub = tsum - m;
				if(tsub > 0) {
					if(sum > tsum) {
						sum = tsum;
						map[j][next] = input[ii];
					}
					else
						map[j][next] = map[j][i];
				}
				else if(!tsub) {
					Min = 0; break;
				}
				else { // minus
					Min = min(Min, -tsub);
					int tcount = 4 - count;
					int tmul = tcount * input[ii];
					if(tmul >= -tsub) {
						for(int k=0; k <= j; ++k) map[k][next] = input[ii];
						for(int k=j+1; k < count; ++k) map[k][next] = map[k][i];
						for(int k=count; k < 4; ++k) {
							if(sum < array_sum(map, next, k) + input[ii]) break;
							map[k][next] = input[ii];
							++count;
						}
						//input[ii] = 0;
						//i -= 2;
						break;
					}
					else {
						map[j][next] = map[j][i];
					}
				}
			}
			if(!Min) break;
			map_sort(map, next, 4);
			//cout << "--------------------------------------------" << endl;
			//for(int y=0; y < 4; ++y, cout << endl)
			//	for(int x=0; x < n+1; ++x) cout << map[y][x] << ' ';
			//cout << "--------------------------------------------" << endl;
		}
		if(!Min) { cout << m << endl; continue; }
		for(int i=0; i < count; ++i) map[i][0] = input[0];
		for(int i=0; i < n; ++i) {
			sum = array_sum(map, i, count);
			if(sum == m) { Min = 0; break; }
			int next = i + 1, ii = i + 1;
			for(int j=0; j < count; ++j) {
				int tsum = sum - map[j][i] + input[ii];
				int tsub = tsum - m;
				if(tsub > 0) {
					if(sum > tsum) {
						sum = tsum;
						map[j][next] = input[ii];
					}
					else
						map[j][next] = map[j][i];
				}
				else if(!tsub) {
					Min = 0; break;
				}
				else { // minus
					Min = min(Min, -tsub);
					map[j][next] = map[j][i];
				}
			}
			if(!Min) break;
		}
		//cout << "- Answer -" << endl;
		cout << m - Min << endl;
	}
	return 0;
}

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

pair<int,pair<int,int>> P(int a,int b,int c){
	pair<int,pair<int,int>> ret;
	ret.first=a; ret.second.first=b; ret.second.second=c;
	return ret;
}

int main(){
while(1){
	int n,m,a,ans=0;
	vector<int>p,pp;
	scanf("%d%d",&n,&m);
	if(n==0&&m==0)break;
	p.push_back(0);
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		p.push_back(a);
	}
	sort(p.begin(),p.end());
	priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;
	pq.push(P(0,0,0));
	while(!pq.empty()){
		pair<int,pair<int,int>> pai=pq.top(); pq.pop();
		if(pai.first<=m){
			pp.push_back(pai.first);
			pq.push(P(p[pai.second.first]+p[pai.second.second+1],pai.second.first,pai.second.second+1));
			if(pai.second.second==0)pq.push(P(p[pai.second.first+1]+p[pai.second.second],pai.second.first+1,pai.second.second));
		}
	}
	int s=pp.size()-1;
	for(int i=0;i<=s;i++){
		while(pp[i]+pp[s]>m)s--;
		if(ans<pp[i]+pp[s])ans=pp[i]+pp[s];
	}
	printf("%d\n",ans);
}
}

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

using namespace std;

int main(void) {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> va(1, 0);
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      if (a > m) continue;
      va.push_back(a);
    }
    sort(va.begin(), va.end());

    int az = va.size();
    vector<int> vb;
    for (int i = 0; i < az; i++) {
      for (int j = i; j < az; j++) {
        if (va[i] + va[j] > m) break;
        vb.push_back(va[i] + va[j]);
      }
    }
    sort(vb.begin(), vb.end());

    int bz = vb.size(), l = 0, r = 0;
    vector<int> vc;
    while (l < bz) {
      vc.push_back(vb[l]);
      while (r < bz && vb[r] == vb[l]) r++;
      if (vb[r] == vb[l]) break;
      l = r;
    }

    int cz = vc.size(), ans = 0;
    for (int i = 0; i < cz; i++) {
      int p = lower_bound(vc.begin(), vc.end(), m - vc[i]) - vc.begin();
      if (vc[i] + vc[p] <= m) {
        ans = max(ans, vc[i] + vc[p]);
      } else {
        ans = max(ans, vc[i] + vc[p - 1]);
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

int main(){
  long n,m,p[1001];
  while(cin>>n>>m,n||m){
    for(int i=1;i<=n;++i) cin>>p[i];
    p[0]=0;
    vector<long> s(0);
    for(int i=0;i<=n;++i){
      for(int j=0;j<=n;++j) s.push_back(p[i]+p[j]);
    }
    sort(s.begin(),s.end());
    s.erase(std::unique(s.begin(), s.end()), s.end());
    long a=0;
    for(vector<long>::iterator it=s.begin();*it<m/2;++it){
      a=max(a,*it+*(upper_bound(s.begin(),s.end(),m-*it)-1));
    }
    cout<<a<<endl;
  }
  return 0;
}

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

int main()
{
	int n,m,in,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&in);
			P.push_back(in);
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			if(P[i] + *it <= m){
				MAX = max(MAX,P[i] + *it);
			}
		}
	
		printf("%d\n",MAX);
	}
	
	return 0;
}