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----------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
//using namespace--------------------------------------------------------------------------------------------------------------
using namespace std;

//define-----------------------------------------------------------------------------------------------------------------------
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)

//typedef----------------------------------------------------------------------------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
//const------------------------------------------------------------------------------------------------------------------------
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
//global-----------------------------------------------------------------------------------------------------------------------
LL n, m, ans = 0;
VI a;
//#define DEBUG
//function-------------------------------------------------------------------------------------------------------------------
int main() {
	while ( scanf( "%lld %lld", &n, &m ) && n && m ) {
		a.resize( n );
		rep( i, n ) {
			scanf( "%lld", &a[ i ] );
		}
		ans = 0;
		sort( a.rbegin(), a.rend() );
		rep( i, n ) {
			while ( ans <= m ) {
				ans += a[ i ];
			}
			ans -= a[ i ];
		}
		printf( "%lld\n", ans );
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
	int n, m;
	int p;
	vector<int> g;
	cin >> n >> m;
	for (int i = 0; i < n; i++){
		cin >> p;
		g.push_back(p);
	}
	for (int i = 0; i < n; i++){
		for (int i2 = 0; i2 < n; i2++){
			g.push_back(g[i]+g[i2]);
		}
	}
	int x, y;
	x = 0; y = g.size() - 1;
	int maxv = 0;
	sort(g.begin(), g.end());
	while (1){
		if (g[x] + g[y] > maxv&&g[x] + g[y] <= m)maxv = g[x] + g[y];
		if (g[x] + g[y] <= m)x++;
		if (g[x] + g[y] > m)y--;
		if (x > y)break;
	}
	cout << maxv << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#define N1 1001
using namespace std;
int Search(int);
int N,M,P[N1];
vector<int> V,cp,cp1;
int main(){
  int i,j,maxv,r;
  while(1){
    cin >> N >> M;
    if(N==0&&M==0) break;
    for(i=0;i<N;i++){
      cin >> P[i];
      V.push_back(P[i]);
    }
    V.push_back(0);
    for(i=0;i<N;i++){
      for(j=i;j<N;j++){
	V.push_back(P[i]+P[j]);
      }
    }
    cp=cp1=V;
    for(i=0;i<cp.size();i++){
      for(j=0;j<cp1.size();j++){
	V.push_back(cp[i]+cp1[j]);
      }
    }
    for(i=0;i<N;i++){
      for(j=0;j<cp.size();j++){
	V.push_back(P[i]+cp[j]);
      }
    }
    sort(V.begin(),V.end());
    maxv=0;
    for(i=0;i<V.size();i++){
      r=Search(V[i]);
      maxv=max(maxv,r);
    }
    cout << maxv << endl;
    V.clear();
    cp.clear();
    cp1.clear();
  }
  return 0;
}
int Search(int a){
  int left=0,right=V.size()-1,mid;
  while(1){
    //x    cout << left << ' ' << right << endl;
    mid=(right+left)/2;
    if(a+V[mid]<M) left=mid+1;
    else if(a+V[mid]>=M) right=mid;
    if(left==right) break;
  }
  return V[left-1];
}

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,ite2;

  for(int i = 0; i < V[0].size(); i++)
    for(ite1 = V[1].begin(); 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 < MAX; i++){
    vector<ll> :: iterator ite;
    ite = upper_bound(V[i].begin(),V[i].end(),M);
    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 <stdio.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>

using namespace std;


void func(int N,int M){
    vector<int> First,Second;

    First.push_back(0);
    int tmp,max = 0;

    for(int i = 0; i < N; i++){
        scanf("%d",&tmp);
        if(tmp <= M){
        	First.push_back(tmp);
        }
    }

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

    Second.push_back(0);
    for(int i = 0; i < First.size();i++){
    	for(int k = i; k < First.size();k++){
    		if(First[i]+First[k] <= M){
    			Second.push_back(First[i]+First[k]);
    		}
    	}
    }

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

    for(int i = 0; i < Second.size();i++){
    	for(int k = i; k < Second.size();k++){
    		if(Second[i]+Second[k] <= M && Second[i]+Second[k] > max){
    			max = Second[i]+Second[k];
    		}
    	}
    }

    printf("%d\n",max);

}

int main(){

    int N,M;

    while(true){
        scanf("%d %d",&N,&M);
        if(N == 0 && M == 0)break;

        func(N,M);
    }

    return 0;
}