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<iostream>
#include<algorithm>
using namespace std;
int main(){
  int n,m;
  while(cin>>n>>m){
    if(n==0&&m==0) return 0;
    int data[n+1],total,ans = 0;
    for(int i=0;i<n;i++) cin >> data[i];
    data[n] = 0;
    sort(data,data+n,greater<int>());
    for(int j=0;j<=n;j++){
      if(data[j]>m) continue;
      total = data[j];
      for(int k=0;k<=n;k++){
	if(total+data[k]>m) continue;
	total += data[k];
	for(int i=0;i<=n;i++){
	  if(total+data[i]>m) continue;
	  total += data[i];
	  for(int o=0;o<=n;o++){
	    if(total+data[o]>m) continue;
	    if(total+data[o]>ans) ans = total+data[o];
	  }
	  total -= data[i];
	}
	total -= data[k];
      }
      total -= data[j];
    }
    cout << ans << endl;
  }
}

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

#define MAX_N 1000

using namespace std;

typedef long long int int64;

int64 N,M,P[MAX_N];
vector<int64> T,S;

int main(void)
{
	while(true)
	{
		for(int i = 0;i < MAX_N;i++)
			P[i] = 0;
		cin >> N >> M;
		if(N == 0 && M == 0)
			exit(0);
		for(int i = 0;i < N;i++)
			cin >> P[i];
		T.push_back(0);
		for(int i = 0;i < N;i++)
		{
			for(int ii = 0;ii < N;ii++)
				T.push_back(P[i] + P[ii]);
		}
		sort(T.begin(),T.end());
		int64 prev = -1;
		for(int i = 0;i < N*N;i++)
		{
			if(prev != T[i])
			{
				S.push_back(T[i]);
				prev = T[i];
			}
		}
		sort(S.begin(),S.end());
		int64 maxscore = 0;
		int64 sc = 0;
		for(auto itr = S.begin();itr < S.end();itr++)
		{
			sc = *(upper_bound(S.begin(),S.end()-1,M-(*itr))-1) + *itr;
			maxscore = max(maxscore,sc);
		}
		cout << maxscore << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

int n;
llong m;

llong bs(vector<llong> v, llong t){
	int l=0, r=v.size()-1;
	int mid;
	while(l<r){
		mid=(l+r)/2;
		if(v[mid]>t) r=mid-1;
		else if(v[mid]==t) return v[mid];
		else{
			if(v[mid+1]>t) return v[mid];
			else l=mid+1;
		}
	}
	
	return v[l];
}

llong solve(vector<llong> p){
	vector<llong> v;
	set<llong> st;
	int c=0;
	REP(i, n){
		REPR(j, n-1){
			if(st.find(p[i]+p[j])==st.end()){
				v.push_back(p[i]+p[j]);
				st.insert(v[c]);
				c++;
			}
		}
	}
	VSORT(v);
	
	llong res=0;
	REP(i, c){
		if(v[i]>m) continue;
		res=max(res, v[i]+bs(v, m-v[i]));
	}
	return res;
}

int main(){
	while(1){
		cin >> n >> m;
		if(n==0 && m==0) return 0;
		n++;
		vector<llong> p(n);
		p[0]=0;
		FOR(i, 1, n) cin >> p[i];
		VSORT(p);
		cout << solve(p) << endl;	
	}
}

C++: #include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20) << fixed;

  int n, m;

  while (cin >> n >> m) {
    if (!n && !m) { break; }
    vector<int> p(n+1, 0);
    rep(i, n) { cin >> p[i]; }

    vector<int> sum_two;

    rep(i, n+1) rep(j, n+1) sum_two.push_back(p[i]+p[j]);
    sort(sum_two.begin(), sum_two.end());

    int ans = 0;
    rep(a, n) {
      rep(b, n) {
        int sum = p[a]+p[b];
        if (sum <= m) {
          auto it = upper_bound(sum_two.begin(), sum_two.end(), m-sum); 
          if (it != sum_two.begin()) { --it; }
          ans = max(ans, sum+(*it));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

int p[1010];
int sum[1002010];

int main(void){
	int n,m;

	while(scanf("%d%d",&n,&m) && (n||m)){
		for(int i=0;i<n;i++){
			scanf("%d",&p[i]);
		}
		p[n++] = 0; //0ツ点ツづ個スツコツアツづーツ催ャツづゥ

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

		int ans = 0;

		for(int i=0;i<size;i++){
			int rem = m - sum[i];
			if(rem < 0) continue;
			int left = 0, right = size;

			while(left < right){
				int center = (left + right) / 2;
				if(rem > sum[center]){
					left = center + 1;
				}
				else if(rem < sum[center]){
					right = center;
				}
				else {
					break;
				}
			}

			ans = max(ans, sum[i] + sum[right-1]);
		}

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

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long n;
	unsigned long long m;
	while(cin>>n>>m,n!=0||m!=0){
		int k=0;
	unsigned long long  s=0;
		vector<unsigned long long> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0&&k!=4){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
				k++;
			}
			else
				n--;
			
		}
		cout<<s<<endl;
	}
	
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
int n,m;
int num[1001];
int sum[1000005];
int main(){
	while(1){
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
	}
	int ro=0;
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			sum[ro]=num[i]+num[j];
			ro++;
		}
	}
	int ans=0;
	sort(sum,sum+ro);
	for(int i=0;i<ro;i++){
		int rp=lower_bound(sum,sum+ro,m-sum[i])-sum;
		if(!rp){
			ans=max(ans,sum[i]);
		}else{
			rp--;
			ans=max(ans,sum[i]+sum[rp]);
		}
	}
	printf("%d\n",sum);
}
	return 0;
}

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++)
		{
			ret = max(ret, *(upper_bound(r.begin(), r.end(), M - r[i]) - 1) + r[i]);
		}

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

	return 0;
}