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 <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;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)


typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vvi;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007



const int MAX_N = 1000;
const int MAX_W = 200000000;

int dp[MAX_N + 1][MAX_W + 1][5];
int v[MAX_N], w[MAX_N];
int N,W;


int solve() {
	for(int i = 0; i < N; i++) {
		for(int j = 0; j <= W; j++){
			for(int k = 0; k < 5; k++){
				if(j < w[i] || k <1) {
					dp[i + 1][j][k] = dp[i][j][k];
				}
				else{
					dp[i + 1][j][k] = max(/*max(*/dp[i][j][k]/*,dp[i][j-w[i]][k-1] + v[i])*/, dp[i + 1][j - w[i]][k-1] + v[i]);
				}
			}
		}
	}
	return dp[N][W][4];
}

int main(){
	int n, m;
	while(cin>>n>>m){
		N = n;
		W = m;
		for(int i = 0; i < n; i++){
			cin >> v[i];
			w[i] = v[i];
		}
		cout<<solve()<<endl;
	}
}

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

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)

int dp[5][1024];
int p[1024];
int main(){
	int n,m;
	while(cin>>n>>m,n|m){
		int ret = 0;
		rep(i,n)scanf("%d",&p[i]);
		rep(i,5)rep(j,n+1)dp[i][j]=0;
		
		rep(i,n){
			for(int j=0; j<=i; j++){
				rep(k,4)if( dp[k][j]+p[i]<=m ){
					dp[k+1][i] = max(dp[k+1][i], dp[k][j]+p[i]);
					ret = max(ret,dp[k+1][i]);
				}
			}
		}
		
		/*
		rep(i,n){
			rep(j,5){
				printf("%d:%d  %d\n",i,j,dp[j][i]);
			}
		}
		*/
		
		printf("%d\n",ret);
	}
	return 0;
}