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

long long int n, m;
long long int p[1000];
long long int s;
vector<long long int> a;
vector<long long int> b;

void f(long long int x){
	if(x <= m) s = max(s, x);
}

int main(){
	while(1){
		scanf("%lld %lld", &n, &m);
		if(n == 0 && m == 0) break;
		memset(p, 0, sizeof(p));
		for(int i=0; i<n; i++) scanf("%lld", &p[i]);
		a = b;
		s = 0;

		for(int i=0; i<n; i++){
			f(p[i]);
		}
		for(int i=0; i<n; i++){
			for(int j=0; j<=i; j++){
				if(p[i] + p[j] <= m){
					a.push_back(p[i] + p[j]);
				}
				f(p[i] + p[j]);
			}
		}
		for(int i=0; i<n; i++){
			for(int j=0; j<a.size(); j++){
				f(p[i] + a[j]);
			}
		}
		for(int i=0; i<a.size(); i++){
			for(int j=0; j<a.size(); j++){
				f(a[i] + a[j]);
			}
		}
		printf("%lld\n", s);

	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int a,s,d[1000],f[210000000],g,h,i,j,z,x,c,v;
int main(void)
{
	int high,low,ba,naka,nokori,kore;
	while(1){
		kore=0;
		scanf("%d %d",&a,&s);
		if(a==0&&s==0)
			break;
		d[0]=0;
		for(i=1;i<=a;i++){
			scanf("%d",&d[i]);
		}
		g=0;
		for(i=0;i<=a;i++){
			for(j=i;j<=a;j++){
				f[g]=d[i]+d[j];
				g++;
			}
		}
		sort(f,f+g);
		/*for(i=0;i<g;i++)
			printf("%d\n",f[i]);*/
		for(i=0;i<g;i++){
			v=0;
			low=0;
			high=g-1;
			nokori=s-f[i];
		//	printf("%d %d\n",i,nokori);
			if(nokori>=0){
				while(1){
					if(low>high)
						break;
					naka=(low+high)/2;
				//	printf("%d = %d %d %d %d %d\n",i,f[high],f[naka],f[low],f[i],nokori);
					if(nokori>f[naka]){
						low=naka+1;
					//	printf("%d %d %d low\n",i,nokori,f[naka]);
					}
					if(nokori<f[naka]){
						high=naka-1;
					//	printf("%d %d %d high\n",i,nokori,f[naka]);
					}
					if(nokori==f[naka]){
						ba=f[naka];
						v=1;
						break;
					}
				}
				if(v==0)
					ba=f[high];
			//	printf("%d\n",f[i]+ba);
				if(kore<f[i]+ba)
					kore=f[i]+ba;
			}
		}
		printf("%d\n",kore);
	}
	return 0;
}

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

using namespace std;
using lint = long long;

int main() {
    while (true) {
        int N, M;
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        vector<int> P(N);
        for (int i = 0; i < N; i++) {
            cin >> P[i];
        }
        P.emplace_back(0);
        ++N;
        vector<int> P2(N*N);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                P2[i*N+j] = P[i] + P[j];
            }
        }
        sort(P2.begin(), P2.end());
        int ans = 0;
        for (int i = 0; i < N*N; i++) {
            const int index = upper_bound(P2.begin(), P2.end(), M - P2[i]) - P2.begin();
            if (index == 0) continue;
            ans = max(ans, P2[i] + P2[index-1]);
        }
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

int main(void) {
  int N,M;
  while (cin >> N >> M , N) {
    vector<int> p(N);
    for (int i = 0; i < N; ++i) {
      cin >> p[i];
    }
    sort(p.begin(),p.end());
    int res = 0; 
    for (int i = N-1; i >= 0; --i) {
      while (res + p[i] <= M) {
        res += p[i];
      }
    }
    cout << res << endl;
  }
  return 0;
}

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