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

int main(){
	/*cout << "1000 200000000" << endl;
	for(int i=1;i<=1000;i++){
		cout << i << endl;
	}
	cout << "0 0" << endl;
	return 0;*/
	int n,m,t;
	while(cin >> n >> m, n){
		vector<int> bin;
		vector<int> data;
		for(int i=0;i<n;i++){
			cin >> t;
			data.push_back(t);
		}
		
		set<int> dp[4];
		
		for(int i=0;i<n;i++){
			dp[0].insert(data[i]);
		}
		
		for(int k=1;k<4;k++){
			for(int i=0;i<n;i++){
				for(set<int>::iterator it = dp[k-1].begin(); it != dp[k-1].end();it++){
					dp[k].insert((*it)+data[i]);
					bin.push_back((*it)+data[i]);
				}
			}
		}
		
		for(int k=0;k<4;k++){
			for(set<int>::iterator it = dp[k].begin(); it != dp[k].end();it++){
				bin.push_back((*it));
			}
		}
		sort(bin.begin(),bin.end());
		
		cout << *(lower_bound(bin.begin(), bin.end(), m)-1) << endl;
		
		
	}
}

C++: #include<stdio.h>
#include<math.h>
#include<vector>
#include<stack>
#include<set>
#include<string.h>
#include<iostream>
#include<algorithm>
#define MAXSIZE 1005
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int main()
{
    set<int,greater<int> >S;
    vector<int>v;
    int a[MAXSIZE];
    int n,m,ans;
    while(scanf("%d%d",&n,&m),n+m)
    {
        ans = 0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            v.push_back(a[i]),S.insert(a[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
                v.push_back(a[i]+a[j]),S.insert(a[i]+a[j]);
        }
        for(int i=0;i<(int)v.size();i++)
        {
            int num = *S.lower_bound(m-v[i]);
            if(num+v[i] < m)
                ans = max(ans,num+v[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

int main()
{
loop:
  int n, m, s = 0, p[1000], o = 4;
  cin >> n >> m;
  if(!n)
    return 0;
  for(int i = 0; i < n; i++)
    cin >> p[i];
  sort(p, p + n, greater<int>());
  for(int i = 0; i < n; i++)
  {
    int a = (m - s) / p[i];
    s += p[i] * min(a, o);
    o -= min(a, o);
  }
  cout << s << endl;
  goto loop;
}

C++: #include<bits/stdc++.h>
using namespace std;
long int n,m,a,ans;
vector <long int> ss1,aa;

int main(){

    while(1){

    cin >> n >> m;
    if(n==0&&m==0)break;
    ans=0;
    ss1.clear();
    aa.clear();

    aa.push_back(0);
    for(int i=0;i<n;i++)cin >> a ,aa.push_back(a);
    sort(aa.begin(),aa.end());

    for(int i=0;i<=n;i++){
        for(int j=i;j<=n;j++){
            if(aa[i]+aa[j]<=m)ss1.push_back(aa[i]+aa[j]);
        }
    }
    sort(ss1.begin(),ss1.end());

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

    cout << ans << endl;

    }

return 0;
}

C++: //JOI 2008??´???3??? ?????????

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>

using namespace std;

#define MAX -210000000
#define MIN 210000000

int a,b;
int d[1000],c[1000*1000+2],hk=0,mcunt,low,high;
int x,pr=0/*,flg[200000000]*/,m,ff=0;

int main(){ 
	
	scanf("%d %d",&a,&b);
	while(a!=0 && b!=0){
		d[0]=0;
		hk=0;
		ff=0;
		pr=0;
		for(int i=1;i<=a;i++) scanf("%d",&d[i]);
		
		for(int i=0;i<=a;i++){
			for(int j=0;j<=a;j++){
				//if(flg[d[i]+d[j]]!=1){
					c[hk]=d[i]+d[j];
					//printf("%lld\n",c[cunt]);
					hk++;
				/*	flg[d[i]+d[j]]=1;
				
				}*/
			}
		}
		//printf("Sort???\n");
		sort(c,c+hk);
		/*printf("\n");
		for(int i=0;i<hk;i++) printf("%d\n",c[i]);
		printf("\n");
		
		printf("hk=%d\n",hk);
		printf("c[low]=%d c[high]=%d\n",c[hk],c[hk-1]);*/
										 
		for(int i=0;i<hk;i++){
			x=b-c[i];
			//printf("x=%d\n",x);
			low=0;
			high=hk-1;
			while(low<=high){
				m=(low+high)/2;
				if(x==c[m]){
					ff=1;
					break;
				}
				if(x>c[m]) low=m+1;
				else if(x<c[m]) high=m-1;
			}
			//printf("\n%d\n",low);
			if(ff==1){
				pr=c[i]+c[m];
				break;
			}
			if(b>c[i]+c[high] && pr<c[i]+c[high]){
				pr=c[i]+c[high];
			//	printf("%d %d\n",c[i],c[high]);
			}
		}
		printf("%d\n",pr);
		scanf("%d %d",&a,&b);
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define PB push_back
using namespace std;
typedef vector<int>V;
int main(void){
	int n,m;
	int sum;
	int i,j;
	int low,high,mid,flg,key;
	int box[1001];
	V v;
	int MAX;
	while(1){
		cin>>n>>m;
		if(n==0&&m==0)	break;
		v.clear();
		box[0]=0;
		for(i=1;i<=n;i++)	cin>>box[i];
		sort(box,box+n+1);
		for(i=0;i<=n;i++){
			for(j=i;j<=n;j++){
				if(box[i]+box[j]<=m)	v.PB(box[i]+box[j]);
			}
		}
		sort(v.begin(),v.end());
		//for(i=0;i<v.size();i++)	cout<<v[i]<<endl;
		MAX=-1;
		for(i=0;i<v.size();i++){
			sum=v[i];
			if(sum<=m){
				key=m-sum;
				//cout<<key<<endl;
				flg=0;
				low=0;
				high=v.size()-1;
				while(low<=high){
					mid=(low+high)/2;
					if(key<v[mid]){
						high=mid-1;
					}
					else if(key>v[mid]){
						low=mid+1;
					}
					else{
						flg=1;
						break;
					}
				}
				if(flg==1){
					sum+=key;
				}
				else{
					sum+=v[high];
				}
				//cout<<sum<<endl;
				MAX=max(MAX,sum);
			}
		}
		cout<<MAX<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) if(DEBUG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DEBUG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
const bool DEBUG = false;

signed main() {
	int P[1010] = {};
	static int a[2000000] = {}; //1000010
	for (int N, M; cin >> N >> M&&N;) {
		memset(a, 0x3f, sizeof(a));
		rep(i, 0, N)cin >> P[i];
		P[N] = 0;
		N++;
		rep(i, 0, N)rep(j, 0, N) {
			a[i*N + j] = P[i] + P[j];
		}
		dumpc(a);
		sort(a, a + N*N);
		dumpc(a);
		int ans = 0;
		rep(i, 0, N*N) {
			if (a[i] > M)break;
			int add = a[upper_bound(a, a + N*N, M - a[i]) - a - 1];
			ans = max(ans, a[i] + add);
		}
		cout << ans << endl;
	}
	return 0;
}