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 N,M,P[1010];
int dp[1010][5] = {};

int main(){
	while(cin >> N >> M && N>0){
		for(int i=1;i<=N;i++){
			cin >> P[i];
		}
		vector<int> v;
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				v.push_back(P[i]+P[j]);
			}
		}
		sort(v.begin(),v.end());
		int ans = 0;
		for(int i=0;i<v.size();i++){
			if(M-v[i]<0) continue;
			int s = upper_bound(v.begin(),v.end(),M-v[i])-v.begin();
			s--;
			ans = max(ans,v[i]+v[s]);
		}
		cout << ans << endl;
	}
}

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

#define MAX_N 1001

using namespace std;

void Uniq(vector<int> &v){
	for(unsigned int i = 0; i < v.size(); i++){
		int sc = v[i];
		for(unsigned int j = i + 1; j < v.size(); j++ ){
			if(sc == v[j]){
				v.erase( v.begin() + j );
				j--;
			}else if(sc < v[i]){
				break;
			}
		}
	}
}

int BinSearch(vector<int> &v, int search){
	int l = 0;
	int r = v.size();
	int m = 0;
	while( r - 1 > l ){
		m = (r + l) / 2;

		if( v[m] > search ){
			r = m;
		}else if(v[m] == search){
			return m;
		}else{
			l = m;
		}
	}
	return m;
}

int main(void){
	while( true ){
		int index = 0;
		int Max = 0;
		int N, M;
		vector<int> Q;
		vector<int> V;

		cin >> N >> M;
		
		if( N == 0 && M == 0 ){
			break;
		}

		for(int i = 0; i < N; i++){
			int tmp;
			cin >> tmp;
			V.push_back( tmp );
		}
		V.push_back( 0 );
		Uniq( V );

		for(unsigned int i = 0; i < V.size(); i++){
			for(unsigned int j = 0; j < V.size(); j++){
				Q.push_back(V[i] + V[j]);
			}
		}
		//cout << "Sorting..." << endl;
		sort(Q.begin(), Q.end());

		//cout << "Uniqing..." << endl;
		Uniq( Q );

		for(unsigned int i = 0; i < Q.size(); i++){
			//cout << Q[i];
			int optim = BinSearch(Q, M - Q[i]);
			//cout << ' ' << optim << ":" << Q[optim] << '\n';
			if( optim > 0 && Q[i] + Q[optim] > M) optim--;
			Max = max( Max , Q[i] + Q[optim] > M ? 0 : Q[i] + Q[optim]);
		}

		cout << Max << '\n';
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a,b) (abs((a)-(b))<EPS)
int p[1001];
int ans;
vector<int> vec;
int n,m;
int main()
{
	while(1)
	{
		memset(p,0,sizeof(p));
		vec.clear();
		cin >> n >> m;
		ans=0;
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++)
		{
			cin >> p[i];
		}
		for(int i=0;i<n;i++)
		{
			vec.pb(p[i]);
			for(int j=0;j<n;j++)
			{
				vec.pb(p[i]+p[j]);
			}
		}
		sort(vec.begin(),vec.end());
		for(int i=0;i<sz(vec);i++)
		{
			vector<int>::iterator it=upper_bound(vec.begin(),vec.end(),m-vec[i]);
			if(it==vec.begin())continue;
			it--;
			ans=max(*it+vec[i],ans);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

int N, M, arr[1000];

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    while (true) {
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        arr[0] = 0;
        for (int j = 1; j <= N; ++j) {
            cin >> arr[j];
        }
        ++N;
        int arr2[1001*1001];
        for (int j = 0; j < N; ++j) for (int k = 0; k < N; ++k) {
            arr2[N*j+k] = arr[j] + arr[k];
        }
        sort(arr2, arr2+N*N);

        int ans = -1, l = 0, r = 0;
        while (l < N*N-1 || r < N*N-1) {
            int cand = arr2[l] + arr2[r];
            if (cand <= M) {
                ans = max(ans, cand);
            }
            if (l == r || (r < N*N-1 && arr2[l] + arr2[r+1] <= M)) {
                ++r;
            } else {
                ++l;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

typedef long long ll;

ll dp[5];
ll dat[1001];

int main() {
  int n, m;
  
  while(cin >> n >> m && (n|m)) {
    for(int i=0; i<n; i++) cin >> dat[i];
    
    fill(dp, dp+5, 0);
    
    ll ans = 0;
    
    for(int i=0; i<4; i++) {
      for(int j=0; j<n; j++) {
	if(dp[i]+dat[j] <= m) {
	  dp[i+1] = max(dp[i]+dat[j], ans);
	  ans = max(dp[i+1], ans);
	}
      }
    }
    
    cout << ans << endl;
  }
  
  return 0;
}

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

using namespace std;

int m,n;
int a[1002];
int aa[1002001];

int ans,Max;

int main(){
	
	while (1) {
		scanf("%d%d",&n,&m);
		ans=0;
		if (n==0&&m==0) {
			break;
		}
		for (int i=0; i<n; i++) {
			scanf("%d",&a[i]);
		}
		a[n++]=0;
		int size=0;
		for(int c=0;c<n;c++){
			for (int d=0; d<n; d++) {
				aa[size++]=a[c]+a[d];
			}
		}
		sort(aa,aa+size);
		
		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) {
				if (m-a[i]-a[i]>=0) {
					int l=0,r=size;
					Max=0;
					while (l<=r) {
						if (aa[size-1]+a[i]+a[j]<ans) {
							break;
						}
						int k=(l+r)/2;
						if (aa[k]<(m-a[i]-a[j])&&aa[k]>Max) {
							Max=aa[k];
						}
						if(aa[k]<(m-a[i]-a[j]))l=k+1;
						else r=k-1;
					}
					if (Max+a[i]+a[j]>ans) {
						ans=Max+a[i]+a[j];
					}
				}
			}
		}
		
		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 = 1000788; T > 1000; 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 += 100000;

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

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