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 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>
#include<cstring>

using namespace std;

typedef pair<int,int> P;
typedef long long ll;
const int MAX = 2;
const ll INF = (1LL<<60);

int N,M;
vector<ll> V[MAX];
ll ans;
void init(){
  for(int i = 0; i < MAX; i++)
    V[i].clear();
  ans = 0;
}


void solve(){
  
  sort(V[0].begin(),V[0].end());

  for(int i = 0; i < V[0].size(); i++)
    for(int j = 0; j < V[0].size(); j++)
      if(V[0][i]+V[0][j] <= M) V[1].push_back(V[0][i]+V[0][j]);

  V[1].push_back(INF);
  sort(V[1].begin(),V[1].end());

   vector<ll> :: iterator ite1;

   /*
  for(int i = 0; i < V[0].size(); i++)
    for(ite1 = V[1].end(); ite1 != V[1].end(); ite1 = upper_bound(V[1].begin(),V[1].end(),*ite1))
      if(*ite1 + V[0][i] <= M) ans = max(ans,*ite1+V[0][i]);
      else break;

  for(ite1 = V[1].begin(); ite1 != V[1].end(); ite1 = upper_bound(V[1].begin(),V[1].end(),*ite1))
    for(ite2 = V[1].begin(); ite2 != V[1].end(); ite2 = upper_bound(V[1].begin(),V[1].end(),*ite2))
      if(*ite1 + *ite2 <= M) ans = max(ans,*ite1+*ite2);
      else break;
   */
   
   for(int i = 0; i < V[0].size(); i++){
     vector<ll> :: iterator ite = lower_bound(V[1].begin(),V[1].end(),M-V[0][i]);
    
     while(1){
       if(V[0][i]+*ite <= M){
	 ans = max(ans,V[0][i]+*ite);
	 break;
       }else if(ite == V[1].begin()) break;
       else ite--;
     }
   }

   for(ite1 = V[1].begin(); ite1 != V[1].end(); ite1 = upper_bound(V[1].begin(),V[1].end(),*ite1)){
     vector<ll> :: iterator ite = lower_bound(V[1].begin(),V[1].end(),M-*ite1);
     while(1){
       if(*ite1+*ite <= M) {
	 ans = max(ans,*ite1+*ite);
	 break;
       }else if(ite == V[1].begin()) break;
       else ite--;
     }
     
   }
   
   for(int i = 0; i < MAX; i++){
     vector<ll> :: iterator ite;
     ite = upper_bound(V[i].begin(),V[i].end(),M);
     /*while(1){
       if(*ite <= M) {
	 ans = max(ans,*ite);
	 break;
       }else if(ite == V[i].begin()) break;
       else ite--;
       }*/
     ans = max(ans,*(ite-1));
   }
   cout << ans << endl;
}

int main(){
  while(cin >> N >> M && N+M){
    init();
    for(int i = 0; i < N; i++){
      ll in;
      cin >> in;
      V[0].push_back(in);
    }
    solve();
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589


#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*) array, (T*) (array + N), val);
}
pii Dir[8] = { //?§????
	{ 0 ,1 },{ -1 ,0 },{ 1 ,0 },{ 0 ,-1 },
	{ 1 ,1 },{ 1 ,-1 },{ -1 ,1 },{ -1 ,-1 }
};

signed main() {
	int n, m;

	while (scanf("%d %d", &n, &m)) {
		if (!n)
			break;
		vector<int> po(n);
		REP(i, n) {
			scnaf("%d", &po[i]);
		}
		po.push_back(0);
		vector<int> toku;
		REP(i, n + 1) {
			REP(j, n + 1) {
				toku.push_back(po[i] + po[j]);
			}
		}
		sort(ALL(toku));
		toku.erase(unique(ALL(toku)),toku.end());
		int ans = 0;
		for (auto itr : toku) {
			auto itr2 = --upper_bound(ALL(toku),m - itr);
			if (itr2 != toku.end())
				ans = max(itr + *itr2, ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

int n, m, point1[1001], sum, ans;
vector<int> point2;

int solve(int start, int end, int border){
    int mid;
    while(1){
        if((end + start) % 2){
            mid = (end + start) / 2 + 1;
        } else{
            mid = (end + start) / 2;
        }

        if(point2[mid] > border){
            end = mid;
        } else{
            start = mid;
        }

        if(end - start == 1){
            if(point2[end] < border){
                return point2[end];
            } else{
                return point2[start];
            }
        }
    }
}

int main(){
    while(cin >> n >> m && n && m){
        for(int i = 0; i < n; i++){
            cin >> point1[i];
        }
        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < n + 1; j++){
                point2.push_back(point1[i] + point1[j]);
            }
        }

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

        for(int i = 0; i < point2.size(); i++){
            if(point2[i] > m){
                continue;
            }
            ans = max(ans, point2[i] + solve(0, point2.size() - 1, m - point2[i]));
        }

        cout << ans << endl;

        memset(point1, 0, sizeof(point1));
        point2.clear();
        ans = 0;
    }

    return 0;
}

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

using namespace std;

int N;
int M;
int P[1000];
int ans=0;

void func(int n,int dart,int sum){
    ans=max(ans,sum);
    if(dart==4 || n<0)return ;
    
    
    if(sum+P[n]<=M){
        func(n-1,dart+1,sum+P[n]);
        func(n,1+dart,sum+P[n]);
        func(n-1,dart,sum);
    }
    else {
        func(--n,dart,sum);
        
    }
    
    return ;
}
int main(){
    while(cin>>N>>M,N,M){
        clock_t start =clock();
        for(int i=0;i<N;i++){
            cin>>P[i];
        }
        sort(P,P+N);
        func(N-1,0,0);
        clock_t end = clock();
        cout<<"time = "<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
        cout<<ans<<endl;
        ans=0;
    }
    
    
}

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

#define MAX_N 1000
int P[MAX_N + 1];
int N, M;
int table[5][MAX_N + 1];

int dfs(int a, int b, int c){
	if(c > M) return P[b] * -1;
	if(a == 0) return P[b];
	if(table[a][b] >= 0) return table[a][b];
	
	int cho = b;
	while(cho <= N){
		int d = dfs(a - 1, cho, c + P[b]) + P[b];
		table[a][b] = max(table[a][b], (d > M)? 0 : d);
		cho++;
	}
	
	return table[a][b];
}

int main(){
	ifstream ifs("input.txt");
	
	for(int i = 0; i < 5; i++){
		for(int j = 0; j <= MAX_N; j++){
			table[i][j] = -1;
		}
	}
	
	ifs >> N >> M;
	
	p[0] = 0;
	for(int i = 1; i <= N; i++) ifs >> P[i];
	
	printf("%d\n", dfs(4, 0, 0));
	
	return 0;
}