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<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 > 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 += 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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
    while(cin){
        int n, m;
        cin >> n >> m;
        if (n == 0 && m == 0) break;
        vector<long long> p(n);
        rep(i, n) {
            cin >> p[i];
        }

        vector<long long> patterns;
        rep(i, n) patterns.push_back(p[i]);
        rep(i, n) rep(j, n) patterns.push_back(p[i] + p[j]);
        sort(patterns.begin(), patterns.end());

        long long ans = 0;
        // rep(i, (int)patterns.size()){
        //     cout << patterns[i] << " ";
        // }
        // cout << endl;

        for (int i = 0; i < (int)patterns.size(); i++) {
            int maxi = upper_bound(patterns.begin(), patterns.end(), m - patterns[i]) - patterns.begin();
            maxi--;
            if (maxi >= 0) {
                ans = max(ans, patterns[i] + patterns[maxi]);
                // cout << i << " " << maxi << endl;
            }
        }
        cout << ans << endl;
    }
}

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

int n;
long m, p[1001], q[1100000];

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

		p[0]=0;
		for(int i=1; i<=n; ++i){
            std::cin >> p[i];
        }
        std::sort(p, p+n);

        long k=0;
		for(int i=0; i<=n; ++i){
            for(int j=i; j<=n; ++j){
		        q[k++]=p[i]+p[j];
            }
        }
        std::sort(q, q+k);

        long ans=0;
		for(long i=0; i<k; ++i){
			long rem=m-q[i];
			if(rem<0) break;

            long l=0, r=k;
			while(l<r){
				long c=(l+r)/2;
				if(rem>q[c]){
					l=c+1;
				}
				else if(rem<q[c]){
					r=c;
				}
				else{
                    break;
                }
			}
			ans=std::max(ans, q[i]+q[r-1]);
		}
	    std::cout << ans << std::endl;
    }
}

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

using namespace std;

int main()
{
	int N, M, D;

	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

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

		vector<int> C;

		for (int i = 0; i < N; i++)
		{
			scanf("%d", &D);

			if (D <= M)
			{
				C.push_back(D);
			}
		}

		vector<int> r; r.push_back(0);

		for (int i = 0; i < C.size(); i++)
		{
			for (int j = 0; j < C.size(); j++)
			{
				if (C[i] + C[j] <= M)
				{
					r.push_back(C[i] + C[j]);
				}
			}
		}

		sort(r.begin(), r.end());

		int ret = 0;

		for (int i = 0; i < r.size(); i++)
		{
			int ptr = upper_bound(r.begin(), r.end(), M - r[i]) - r.begin() - 1;
			
			if (ptr >= 0)
			{
				ret = max(ret, r[i] + r[ptr]);
			}
		}

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

	return 0;
}

C++: #include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

ll M,N,P[1001];

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

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

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

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

		if ( !( n | m ) )
		{
			break;
		}

		VI ps( n );
		FOR( p, ps )
		{
			cin >> p;
		}
		ps.PB( 0 );

		vector<LL> totals;
		REP( i, 0, ps.size() )
		{
			REP( j, i, ps.size() )
			{
				totals.PB( (LL)ps[i] + ps[j] );
			}
		}
		sort( ALL( totals ) );

		LL res = 0;
		REP( i, 0, totals.size() )
		{
			if ( m < totals[i] )
			{
				break;
			}

			int pos = upper_bound( ALL( totals ), m - totals[i] ) - totals.begin();
			if ( totals[i] + totals[ pos - 1 ] <= m )
			{
				res = max( res, totals[i] + totals[ pos - 1 ] );
			}
		}

		cout << res << endl;
	}

	return 0;
}

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

int main(){
  int n,m;

  while(cin >> n >> m , n + m){
    vector<int> v;
    int a;

    for(int i = 0 ; i < n ; i++){
      cin >> a;
      v.push_back(a);
    }

    int size = v.size();
    for(int i = 0 ; i < size ; i++){
      for(int j = 0 ; j < size ; j++){
	v.push_back(v[i]+v[j]);
      }
    }
    
    sort(v.begin(),v.end());

    int ans = 0;
    for(int i = 0 ; i < v.size() ; i++){
      for(int j = 0 ; j < v.size() ; j++){
	if(v[i]+v[j] <= m) ans = max(ans,v[i]+v[j]);
      }
    }

    cout << ans << endl;
  }

  return 0;
}