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

		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<iostream>
#include<vector>
using namespace std;
typedef unsigned int uint;
uint maxv;
void solve(uint now,uint m,uint *a,int n){
  int r=n-1,l=0,mid;
  while(l <=r){
    mid = (l+r)/2;
    if ( a[mid]+now>m)r=mid-1;
    else if ( a[mid]+now <=m)l=mid+1;
    //else break;
    if ( a[mid]+now == m){maxv=m;return;}
  }
  if ( now+a[mid]>maxv&& now+a[mid]<=m)maxv=now+a[mid];
}


main(){
  int n;
  uint m;
  while(scanf("%d%u",&n,&m) && n){
    uint one[n];
    uint two[n*n];
    int p=0;
    maxv=0;
    for(int i=0;i<n;i++)scanf("%u",&one[i]);
    for(int i=0;i<n;i++){
      if ( one[i]>maxv &&one[i] <=m)maxv=one[i];
      for(int j=i;j<n;j++){
	if ( one[i]+one[j]>m)continue;
	two[p++]=one[i]+one[j];
	if (two[p-1]>maxv)maxv=two[p-1];
      }
    }
    sort(one,one+n);
    sort(two,two+p);
    for(int i=0;i<p;i++){
      if ( two[i]+one[0]>m)break;
      if ( two[i]+two[0]<=m)solve(two[i],m,two,p);
      if ( two[i]+one[0]<=m)solve(two[i],m,one,n);
    }
    printf("%u\n",maxv);
  }
  return false;
}

C++: #include <bits/stdc++.h>
//#include <iostream>
using namespace std;

int main() {
  while (true) {
    int N, M;
    vector<int> P;
    cin >> N >> M;
    if (N == 0 && M == 0)
      return 0;
    // x1
    for (int i{}; i < N; ++i) {
      int p;
      cin >> p;
      P.push_back(p);
    }
    // x2
    for (int i{}; i < N; ++i) {
      for (int j{}; j < N; ++j) {
        P.push_back(P[i] + P[j]);
      }
    }
    sort(P.begin(), P.end());
    int ans{};
    for (const auto &m1 : P) {
      if (m1 >= M) {
        if (m1 == M) {
          ans = M;
        }
        break;
      }
      auto it = upper_bound(P.begin(), P.end(), M - m1);
      if (it != P.end() && --it != P.end()) {
        ans = max(ans, m1 + *it);
      }
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;

int main(){
  int N, M;
  while(cin >> N >> M, N){
    vector<int> P(N);
    for(int i=0;i<N;i++)cin >> P[i];
    vector<int> cand;
    for(int i=0;i<N;i++)for(int j=i;j<N;j++)if(P[i] + P[j] <= M){
      cand.push_back(P[i] + P[j]);
    }
    for(int x : P)if(x <= M)cand.push_back(x);
    sort(cand.begin(), cand.end());
    int j = cand.size() - 1;
    int res = cand.back();
    for(int i=0;i<cand.size();i++){
      while(j >= 0 && cand[i] + cand[j] > M)--j;
      if(j >= 0){
        res = max(res, cand[i] + cand[j]);
      }
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long int ll;

int main()
{
	const int MAX_N=1005;
    int n,m,p[MAX_N];
	int x[MAX_N*MAX_N];
	while(true){
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)return 0;
		for(int i=0;i<n;i++)scanf("%d",&p[i]);
		p[n]=0;
		n++;

		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				x[i*n+j]=p[i]+p[j];
			}
		}
		sort(x,x+n*n);

		int ans=0;
		for(int i=0;i<n*n;i++){
			int mp=m-x[i];
			if(mp<0)continue;
			int l=0,r=n*n-1;
			int y=0;
			while(r-l>=1){
				int c=(r+l)/2;
				if(x[c]==mp){
					y=x[c];
					break;
				}
				else if(x[c]<mp){
					y=x[c];
					l=c+1;
				}
				else r=c;
			}
			ans=max(ans,y+x[i]);
		}
		printf("%d\n",ans);
	}
    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> point(n + 1);
		vector<int> sum;
		for (int i = 0; i < n; i++) {
			cin >> point[i];
		}
		for (int i = 0; i < n + 1; i++) {
			for (int j = i; j < n + 1; j++) {
				sum.push_back(point[i] + point[j]);
			}
		}
		sort(sum.begin(), sum.end());

		int ans = 0;
		for (int i = 0; i < sum.size(); i++) {
			int t = m - sum[i];
			vector<int>::iterator it = upper_bound(sum.begin(), sum.end(), t);
			if (it != sum.begin()) {
				ans = max(ans, sum[i] + * (it - 1));
			}
		}
		cout << ans << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#define MAXN 1000

using namespace std;

int N,M;
int pi[MAXN];

bool comp(int i,int j)
{
	return i>j;
}

void solve()
{
	int temp=4;
	int res=0;
	int grade=M;
	sort(pi,pi+N,comp);
	for(int i=0;i<N;i++)
	{
		while(grade>pi[i]&&temp>0)
		{
			grade-=pi[i];
			temp--;
			res+=pi[i];
		}
		if(temp==0)break;
	}
	cout<<res<<endl;
}

void input()
{
	while(scanf("%d%d",&N,&M))
	{
		if(N==0&&M==0)break;
		for(int i=0;i<N;i++)
		{
			scanf("%d",&pi[i]);
		}
		solve();
	}
}

int main()
{
	input();
	return 0;
}

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 = 1760788; 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 += 100000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*1000)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}