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

long N, M;

void solve(){
    cin >> M;
    long P[N + 1]; P[0] = 0;
    for (long i = 1; i < N + 1; i++) cin >> P[i];
    sort(P, P + N + 1);
    long *p = unique(P, P + N + 1);
    N = distance(P, p);
    set<long> s;
    for(long i = 0; i < N; i++){
        for (long j = i; j < N; j++){
            if (P[i] + P[j] > M) break;
            s.insert(P[i] + P[j]);
        }
    }
    long S[s.size()];
    set<long>::iterator it; long i = 0;
    for (it = s.begin(); it != s.end(); it++){
        S[i] = *it;
        i++;
    }
    long ans = 0;
    for (int i = 0; i < s.size(); i++){
        p = upper_bound(S, S + s.size(), M - S[i]);
        if (p == S + 1) break;
        ans = max(ans, S[i] + *(p - 1));
    }
    cout << ans << endl;
    return;
}

int main(){
    while(cin >> N){
        if (N == 0) return 0;
        solve();
    }
}

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

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)
vector<int> t;
vector<int> w;
int main(){
	while(1){
		w.clear();
		t.clear();
	int n,m;
	cin>>n>>m;
	if(n==0)break;
	t.push_back(0);
	
	rep(i,n){
		int a;
		cin>>a;
		t.push_back(a);
	}
	n++;
	sort(t.begin(),t.end());
	rep(i,n){
		rep(j,n){
			w.push_back(t[i]+t[j]);
		}
	}
	sort(w.begin(),w.end());
	int ans=0;
	rep(i,w.size()){
		vector<int>::iterator a=upper_bound(w.begin(),w.end(),m-w[i]);
		if(a==w.begin())continue;
		ans=max(ans,*(a-1)+w[i]);
	}
	printf("%d\n",ans);
}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL<<61;

int p[1005], n, m, nn;
vector<int> gedo;

inline void cut(int *k) {
  if (m < *k) *k = 0;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> n >> m, n) {
    gedo.clear();
    for (int i = 0; i < n; ++i) {
      cin >> p[i];
    }

    for (int i = 0; i < n; ++i) {
      for (int j = i; j < n; ++j) {
        gedo.PB(p[i] + p[j]);
      }

      gedo.PB(p[i]);
    }
    gedo.PB(0);
    sort(gedo.begin(), gedo.end());
    gedo.erase(unique(gedo.begin(), gedo.end()), gedo.end());
    gedo.PB(INF);

    int ans = 0;
    for (int i = 0; i < gedo.size() - 1; ++i) {
      int l = 0, r = gedo.size() - 1, cur = gedo[i];

      while (1 <= r - l) {
        int j = (r + l) / 2, temp1 = cur + gedo[j], temp2 = cur + gedo[j + 1];
        cut(&temp1);
        cut(&temp2);
        ans = max(ans, temp1);

        if (temp1 != 0 && temp2 == 0) break;

        if (temp1 == 0) {
          r = j;
        } else {
          l = j + 1;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

int main() {
  while(1) {
    int n, m, a[1000]={},check=0,max=0,v[1000000]={},k=0;
    cin >> n >> m;
    if(n==0&&m==0) break;

    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=0; i<=n; i++) {
      for(int j=i; j<=n; j++) {
	v[k]=a[i]+a[j];
	k++;
      }
    }
    sort(v, v+k);

    for(int i=0; i<k; i++){
      if (v[i]>m) break;      
      int t=distance(v,upper_bound(v, v+k, m-v[i]));
      if(v[i]+v[t]<=m && max<v[i]+v[t]) max = v[i]+v[t];
      if(t+1<k && v[i]+v[t+1]<=m && max<v[i]+v[t+1]) max = v[i]+v[t+1];
      if(t-1>=0 && v[i]+v[t-1]<=m && max<v[i]+v[t-1]) max = v[i]+v[t-1];
    }
    cout << max << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n,m;
  while(cin>>n>>m,n){
    int p[n];
    for(int i=0;i<n;i++) cin>>p[i];
    set<int> s;
    s.insert(0);
    s.insert(m+1);
    for(int i=0;i<n;i++)
      s.insert(p[i]);
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	s.insert(p[i]+p[j]);
    int ans=0;
    for(int x:s){
      if(x>m) break;
      int y=*--s.upper_bound(m-x);
      //cout<<x<<" "<<y<<endl;
      assert(x+y<=m);
      ans=max(ans,x+y);
    }
    cout<<ans<<endl;
  }
  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 = 1060788; 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;
}

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

int n,m;
long long target[1000];
int sum = 0;

void chek(int cnt, int point,int now)
{
	int i;
	//int sum=0;
	sum = max(sum, point);
	if (cnt == 5)
	{
		return ;
	}
	if (sum == m)
	{
		return;
	}

	for (i = now; i <= n; i++)
	{
		if (point + target[i] <= m)
		{
			chek(cnt + 1, point + target[i],i);
		}
		else return;
	}

}

int main()
{
	//int sum;
	int i;

	while (scanf("%d %d",&n,&m), n != 0 || m != 0)
	{
		sum = 0;
		//printf("AA%d\n",m);
		for (i = 1; i <= n; i++)
		{
			scanf("%d", &target[i]);
		}

		sort(target + 1, target + n + 1);

		chek(1, 0,0);

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

	return 0;
}

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

int max_sum(int a[],int as,int b[],int bs,int m){
	int j=bs-1;
	int res=0;
	for(int i=0;i<as;i++){
		while(0<=j && m<a[i]+b[j])j--;
		if(j==-1)break;
		res=max(res,a[i]+b[j]);
	}
	return res;
}

int p[1000],p2[1000*1001/2];
int main(){
	int n,m;
	while(cin>>n>>m,n||m){
		int ans=0;
		for(int i=0;i<n;i++){
			cin>>p[i];
			if(p[i]<=m)ans=max(ans,p[i]);
		}
		sort(p,p+n);
		int n2=0;
		for(int i=0;i<n;i++){
			for(int j=i;j<n;j++){
				int a=p[i]+p[j];
				p2[n2++]=a;
				if(a<=m)ans=max(ans,a);
			}
		}
		sort(p2,p2+n2);
		ans=max(ans,max_sum(p,n,p2,n2,m));
		ans=max(ans,max_sum(p2,n2,p2,n2,m));
		cout<<ans<<endl;
	}
	return 0;
}