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 <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 <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> a;
vector <int> all;
int n,m;


int main() {

  while(1) {
    scanf("%d%d",&n,&m);
    if(n == 0 && m ==0) break;
    a.resize(n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);

    a.push_back(0);
    all.clear();

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

    sort(all.begin(),all.end());

    int ans = 0;
    int allsize = all.size();
    int b = 0;
    for(int i=allsize-1;i>= 0;i--) {
      for(int j=b;j<allsize;j++) {
	int sum = all[i] + all[j];
	if(sum > m) break;
	b++;
	if(ans < sum) ans = sum;
      }
      if(ans == m) break;
    }

    cout << ans <<endl;
  }
  return 0;
  
}

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

int n, m;
vector<int> p;
int t;
vector<int> q;
vector<int> o;
int ans;

int b(int x){
	int l = 0;
	int r = q.size();
	while(r - l > 1){
		int i = (l + r) / 2;
//		printf("l=%d, r=%d, i=%d, q[i]=%d, x=%d\n", l, r, i, q[i], x);
		if(q[i] <= x){
			l = i;
		}else{
			r = i;
		}
	}
//	printf("%d\n", q[l]);
	return q[l];
}

int main(){
	while(1){
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;
		p = o;
		p.push_back(0);
		for(int i=0; i<n; i++){
			scanf("%d", &t);
			p.push_back(t);
		}
		q = o;

		for(int i=0; i<p.size(); i++){
			for(int j=0; j<p.size(); j++){
				q.push_back(p[i] + p[j]);
			}
		}
		sort(q.begin(), q.end());
/*
		for(int i=0; i<q.size(); i++)
			printf("%d\n", q[i]);
*/
		for(int i=0; i<q.size(); i++){
			int a = q[i];
			if(m - a < 0) continue;
			ans = max(ans, b(m - a) + a);
//			printf("%d\n", b(m - a));
		}

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

	return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
//http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
#include<cstring>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define P pair<int,int>
#define ll long long 

ll n,m;
ll t[10000];
const int MA=2000000;
ll s1[MA];
ll s2[MA];


int main(){

while(cin>>n>>m,n||m){
rep(i,MA)s1[i]=s2[i]=0;
rep(i,10000)t[i]=0;

for(int i=1;i<=n;i++)cin>>t[i];
int g=0;

for(int i=0;i<=n;i++){
	for(int j=0;j<=n;j++){
		s1[i*(n+1)+j]=t[i]+t[j];
		g++;
	}
}


sort(s1,s1+g);
rep(i,g)s2[i]=s1[i];

ll ans=0;

for(int i=0;i<g;i++){
	int now=s1[i];

	int l=0;
	int r=g-1;
	int mid=(l+r)/2;
	ll x=s2[mid];

	for(int i=0;i<70;i++){
		if(x+now==m)break;
		if(l>r)break;
		if(x+now>m){
			r=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
		else{
			l=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
	}

	//cout<<" "<<x<<" "<<now<<" "<<x+now<<endl;
	if(x+now<=m)
	ans=max(ans,x+now);
}
cout<<ans<<endl;
}
	return 0;
}

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;
		else if(v[mid]<t){
			if(v[mid+1]>t) return v[mid];
			else l=mid+1;
		}
		else break;
	}
	
	return v[r-1];
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  while (true) {
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    int P[N + 1];
    P[0] = 0;
    int ans = 0;
    for (int i = 0; i < N; i++) {
      cin >> P[i + 1];
    }
    vector<int> v;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        if (P[i] + P[j] <= M) v.push_back(P[i] + P[j]); 
      }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
      int res = M - v[i];
      if (res == 0) {
        ans = M;
        break;
      }
      int left = -1;
      int right = v.size();
      while (right - left > 1) {
        int mid = (right + left) / 2;
        if (res >= v[mid]) left = mid;
        else right = mid;
      }
      ans = max(ans, v[i] + v[left]);
    }
    cout << ans << "\n";
  }
  return 0;
}

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

using namespace std;

const int INF = 1<<30;

int N,M;

int main(){
  while(scanf("%d%d", &N,&M) && N||M){
    vector<int>a,v;
    a.push_back(0);
    for(int i=0;i<N;i++){
      int x;scanf("%d", &x);
      a.push_back(x);
    }
    for(int i=0;i<=N;i++) for(int j=0;j<=N;j++){
	if(a[i]+a[j] <= M) v.push_back(a[i]+a[j]);
    }
    v.push_back(INF);

    sort(v.begin(), v.end());
    int res = 0;
    for(int i=0;i<v.size()-1;i++){
      int x = M-v[i];
      vector<int>::iterator it = upper_bound(v.begin(), v.end(), x); if(it != v.begin()) it--;
      res = max(res, v[i] + *it);
    }
    printf("%d\n", res);
  }
  return 0;
}