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<vector>
#include<algorithm>
using namespace std;

int main(){
while(1){
	int n,m;
	vector<int> p,q;
	scanf("%d%d",&n,&m);
if(n==0&&m==0)break;
	q.push_back(0);
	for(int i=0;i<n;i++){
		int a;
		scanf("%d",&a);
		p.push_back(a);
		q.push_back(a);
	}
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			q.push_back(p[i]+p[j]);
		}
	}
	sort(q.begin(),q.end());
	int ans=0;
	int f=0,s=q.size()-1;
	while(1){
		if(q[f]+q[s]>ans&&q[f]+q[s]<=m)ans=q[f]+q[s];
		if(q[f]+q[s]<=m)f++;
		if(q[f]+q[s]>m)s--;
		if(f>s)break;
	}
	printf("%d\n",ans);
}
}

C++: 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	int ans[4];
	int temp;
	double energy,energy_min,energy_now;
	int M, N;
	int* p;
	int i, j;
	double T=100;
	int ret;
	while (1) {
		scanf("%d%d", &N, &M);
		p = (int*)malloc((N+1) * sizeof(int));
		for (i = 0; i < N; i++) {
			scanf("%d", &p[i]);
		}
		p[N] = 0;
		if (N == M && N == 0) {
			break;
		}
		energy_min = pow(10, 300);
		for (j = 0; j < 4; j++) {
			ans[j] = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
		}
		
		for (T = 1000000; T > 100; T--) {
			for (j = 0; j < 4; j++) {
				temp = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
				for (j = 0; j < 4; j++) {
					ret = ans[0] + ans[1] + ans[2] + ans[3];
					ret += temp - ans[j];
					energy = M - ret;
					if (M < ret)energy += 100000000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*10)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}

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

int n, m;
int p[1000];
int ans;
set<int> s;
set<int>::iterator it;
vector<int> v;

void solve(){
	ans = 0;
	v.clear();
	s.clear();
	s.insert(0);
	for(int i = 0; i < n; i++){
		scanf("%d", &p[i]);
		if(p[i] <= m) s.insert(p[i]);
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			if(p[i]+p[j] <= m) s.insert(p[i]+p[j]);
		}
	}
	for(it = s.begin(); it != s.end(); it++) v.push_back(*it);
	int j = v.size()-1;
	for(int i = 0; i < v.size(); i++){
		while(true){
			if(v[i]+v[j] <= m){
				ans = max(ans,v[i]+v[j]);
				break;
			}
			j--;
		}
	}
	printf("%d\n", ans);
}

int main(){
	while(scanf("%d%d", &n, &m),n||m) solve();
}

C++: /**
 * File    : JOI_2007_darts.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-3-8 20:45:20
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
int P[1010];
vector<int> S;

int main()
{
  while (cin >> N >> M && N && M)
  {
    S.clear();
    for (auto i = 0; i < N; i++)
    {
      cin >> P[i];
    }
    P[N] = 0;
    N++;
    for (auto i = 0; i < N; i++)
    {
      for (auto j = i; j < N; j++)
      {
        int sum = P[i] + P[j];
        if (sum <= M)
        {
          S.push_back(sum);
        }
      }
    }
    sort(S.begin(), S.end());
    int ans = 0;
    int l = 0;
    int r = S.size() - 1;
    while (l <= r)
    {
      int sum = S[l] + S[r];
      if (sum <= M)
      {
        ans = max(ans, sum);
        l++;
      }
      else
      {
        r--;
      }
    }
    cout << ans << endl;
  }
}

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

using namespace std;

typedef vector<int> vi;

#define PB push_back
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define all(a) a.begin(),a.end()

int P[1001];

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int N, M;
	
	while (cin >> N >> M, N || M) {
		rep1(i, N) cin >> P[i];

		vi A;

		rep(i, N + 1) rep(j, i + 1) if (P[i] + P[j] <= M) A.PB(P[i] + P[j]);

		sort(all(A));

		int ma = 0;

		int l = 0, r = A.size() - 1;

		while (l < r) {
			while (A[l] + A[r] > M) r--;
			ma = max(ma, A[l] + A[r]);
			l++;
		}

		cout << ma << endl;
	}
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> *points;
vector<int> *twice;

void deldup(vector<int> *vec) {
    vector<int>::iterator it = vec->begin();
    bool is_straight = false;
    int save = -1;

    while (it != vec->end()) {
        if (*it == save) {
            vec->erase(it++);
            is_straight = true;
            continue;
        } else {
            save = *it;
            if (is_straight) {
                vec->erase(it++);
                is_straight = false;
                continue;
            }
            is_straight = false;
        }
        it++;
    }
}

int search(vector<int> *target, int standard) {
    int ind;
    int min = 0;
    int max = target->size() - 1;

    while (1) {
        ind = (min + max)/2;

        if (max - min <= 1) return target->at(min);

        if (target->at(ind) <= standard) {
            min = ind;
        } else {
            max = ind;
        }
    }
}

int main() {
    while (1) {
        int i;
        int n, m;
        int max = 0;

        scanf("%d %d", &n, &m);
        if (n == 0 && m == 0) return 0;

        points = new vector<int>();
        twice = new vector<int>();

        points->push_back(0);
        for (i=0;i<n;i++) {
            int k;
            scanf("%d", &k);
            points->push_back(k);
        }

        vector<int>::iterator it1 = points->begin();
        while (it1 != points->end()) {
            vector<int>::iterator it2 = points->begin();
            while (it2 != points->end()) {
                twice->push_back(*it1 + *it2);
                it2++;
            }
            it1++;
        }

        sort(points->begin(), points->end());
        sort(twice->begin(), twice->end());

        vector<int>::reverse_iterator rit1 = twice->rbegin();
        while (rit1 != twice->rend()) {
            if (*rit1 <= m) {
                int got = *rit1 + search(twice, m - *rit1);
                if (max < got) max = got;
            }
            rit1++;
        }

        printf("%d\n", max);
    }
}