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 _, val := range sums {
			rem := M - val
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, val+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

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

#define ef(e, a, b) ((e) ? (a) : (b))

using namespace std;

int main ( void )
{
	int n, m;
	int p[4][1000];

	while ( cin >> n >> m, (n | m) )
	{
		int ans = 0;
		
		for (int i = 0; i < n; ++i)
		{
			cin >> p[0][i];
			
			ans = max(ans, ef((p[0][i]*4 == m) || (p[0][i]*3 == m) || (p[0][i]*2 == m), m, ef(p[0][i] > m, 0, p[0][i])));
			
			for (int j = 1; j < 4; ++j) { p[j][i] = 0; }
		}
		
		for (int i = 1; i < 4; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				for (int k = 0; k < n; ++k)
				{
					int t = p[i-1][j] + p[0][k];
					p[i][j] = max(p[i][j], ef(t > m, 0, t));
					ans = max(ans, p[i][j]);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
typedef long long L;
using namespace  std;
int main(){
	int n;
	L m,p[1001],sum;
	vector<L> sum1;
	vector<L>::iterator it,ele;
	scanf("%d %lld",&n,&m);
	while(n!=0&&m!=0){
		sum=0;
		for(int i=0;i<n;i++){
			scanf("%lld",&p[i]);
		}
		p[n]=0;
		for(int i=0;i<=n;i++){
			for(int i2=0;i2<=n;i2++){
				sum1.push_back(p[i]+p[i2]);
			}
		}
		sort(sum1.begin(),sum1.end());
		for(it=sum1.begin();it<sum1.end();it++){
			if(*it>m)
				break;
			ele=upper_bound(sum1.begin(),sum1.end(),(m-(*it) ) );
			if(ele!=sum1.begin()){
				ele--;
			}
			if(sum<=(*it)+(*ele)){
				sum=(*it)+(*ele);
			}
			if(sum==m)
				break;
		}
		printf("%lld\n",sum);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 1005
#define LSIZE 1100005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;
ll M;
ll T[SIZE];
ll S[LSIZE];

ll solve()
{
	scanf("%d %lld",&n,&M);
	if(n+M ==0)return -1;
	for(int i=1;i<=n;i++)scanf("%lld",&T[i]);
	T[0]=0;
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			S[i*(n+1)+j] = T[i]+T[j];
		}
	}
	ll maxv=0;
	ll v;
	sort(S,S+(n+1)*(n+1));
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			if(T[i]+T[j]<=M){
				v = T[i]+T[j]+S[upper_bound(S,S+(n+1)*(n+1),M-T[i]-T[j])-S-1];
				maxv = max(maxv,v);
			}
		}
	}
	return maxv;
}

int main()
{
	while(1){
		ll ans = solve();
		if(ans==-1)break;
		else printf("%lld\n",ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 1004

int p[MAX], q[MAX*MAX];
int main(){
	int n, m, ans, qq;
	while(cin >> n >> m, n){
		for(int i = 1;i <= n;i++)scanf("%d", &p[i]);
		ans = 0;p[0] = 0;qq = 0;
		
		for(int i = 0;i <= n;i++)
			for(int j = 0;j <= n;j++)
				if(p[i]+p[j] <= m)q[qq++] = p[i] + p[j];
		
		sort(q, q+qq);
		int t;
		for(int i = 0;i < qq;i++){
			t = upper_bound(q, q+qq, (m-q[i]))-q;
			if(t-1 >= 0)ans = max(ans, q[i]+q[t-1]);
		}
		cout << ans << endl;
	}
	return 0;
}

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

vector<long long int> p;
vector<long long int> p2;

int main(void){
	int a,b,n,m;
	while(1){
		long long int ans=0;
		scanf("%d%d",&n,&m);
		if(n==0 && m==0)break;
		p.clear();
		p2.clear();
		for(a=0;a<n;a++){
			int num;
			scanf("%d",&num);
			p.push_back(num);
		}
		sort(p.begin(),p.end());
		p2.push_back(0);
		for(a=0;a<n;a++){
			p2.push_back(p[a]);
			for(b=0;b<n;b++){
				p2.push_back(p[a]+p[b]);
			}
		}
		p2.push_back(0);
		sort(p2.begin(),p2.end());
		for(a=0;a<p2.size();a++){
			long long int res=m-p2[a];
			if(m-p2[a]<0)continue;
			long long int pp=*(lower_bound(p2.begin(),p2.end(),res)-1);
			if(ans<pp+p2[a])ans=pp+p2[a];
			//cout << pp+p2[a] << endl;
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

using namespace std;

int main(void)
{
   int n,m,input,s;
   vector<int>a;
   while(scanf("%d%d",&n,&m) ,n,m)
   {
      a.clear();
      a.push_back(0);
      for(int i = 0;i < n;i++)
      {
         scanf("%d",&input);
         a.push_back(input);
      }

      s = a.size();
      for(int i = 0;i < s;i++)
         for(int j = 0;j < s;j++)
            if(a[i] + a[j] <= m) a.push_back(a[i]+a[j]);

      s = a.size();
      for(int i = 0;i < s;i++)
      {
         for(int j = 0;j < s;j++)
            if(a[i]+a[j] < m) a.push_back(a[i]+a[j]);
      }

      s = a.size();
      int most = 0;
      for(int i = 0;i < s;i++)
      {
         if(most < a[i]) most = a[i];
      }

      printf("%d\n",most);
   }


   return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define INF (2000000000)
#define MOD (1000000007)
using namespace std;

typedef vector<int> VI;
typedef vector<int>::iterator VII;
typedef vector<double> VD;
typedef vector<double>::iterator VDI;
typedef vector<long> VL;
typedef vector<long>::iterator VLI;
typedef pair<int, int> P;

long a[1001];
VL score;

int main()
{
	int N;
	long M;
	
	while (scanf("%d %ld", &N, &M), N + M){
		for (int i = 1; i <= N; i++){
			scanf("%ld", &a[i]);
		}
		for (int i = 0; i <= N; i++){
			for (int j = 0; j <= N; j++){
				score.push_back(a[i] + a[j]);
			}
		}
		sort(score.begin(), score.end());
		
		int size = score.size();
		long maxi = 0;
		int l = 0;
		int r = size - 1;
		while (l < r){	
			if (score[l] + score[r] <= M){
				maxi = max(maxi, score.at(l) + score.at(r));
				l++;
			}
			else {
				r--;
			}
		}
		
		printf("%ld\n", maxi);
		score.clear();
	}
	
	return 0;
}