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++: //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 __int64

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

int ya[4];

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];
for(int i=0;i<=n;i++){
	for(int j=0;j<=n;j++){
		s1[i*(n+1)+j]=t[i]+t[j];
		s2[i*(n+1)+j]=t[i]+t[j];
	}
}
int g=(n+1)*(n+1);

sort(s1,s1+g);
sort(s2,s2+g);

//rep(i,g)cout<<s1[i]<<" ";cout<<endl;

int 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;
	int x=s2[mid];

	for(int i=0;i<60;i++){
		if(x+now==m)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<algorithm>
#include<vector>
using namespace std;
int main(){
	while(1){
	int n,m,p,q=0,r=0,l=0,s=0,e=0,ans=0;
	cin>>n>>m;
		if(n==0 && m==0)
			break;
	vector<int> a,x;
	a.push_back(0);
	for(int i=0;i<n;i++){
		cin>>p;
		a.push_back(p);
	}
	q=n+1;
	//cout<<q<<endl;
	for(int i=0;i<q;i++){
		for(int j=0;j<q;j++){
			x.push_back(a[i]+a[j]);
		}
	}
	sort(x.begin(),x.end());
	//for(int i=0;i<q*q;i++)
	//cout<<x[i]<<' ';
	//cout<<endl;
	for(int i=0;i<x.size();i++){
		r=m-x[i];
		//cout<<r<<"を探します"<<endl;
		if(r>=0){
			s=0;
			e=q*q;
			l=(s+e)/2;
			while(s<e){
				//cout<<"l="<<l<<endl;
				l=(s+e)/2;
				if(x[l]==r)break;
				if(x[l]>r){
					e=l-1;
				}else{
					s=l+1;
				}
			}
			while(x[l]>r&&p>0)
			p--;
			while(1){
				if(l+1==q*q)break;
				if(x[l+1]<=r)l++;
				else break;
			}
		ans=max(ans,x[i]+x[l]);
		}
	}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

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

// 0119 B-0563 18:00 ~ 19:00

int main() {
	int n, m;
	while(cin >> n >> m) {
		if(n == 0 && m == 0) break;

		vector<ll> v(n);
		rep(i, n) cin >> v[i];

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

		vector<ll> p;
		rep(i, n) p.push_back(v[i]);

		rep(i, n) {
			REP(j, i, n) {
				p.push_back(v[i] + v[j]);
			}
		}

		sort(p.begin(), p.end());
		ll ans = 0;
		rep(i, p.size()) {
			int id = upper_bound(p.begin(), p.end(), m - p[i]) - p.begin();
			if(id == 0) {
				if(p[i] <= m) ans = max(ans, p[i]);
			}
			else ans = max(ans, p[i] + p[id-1]);
		}

		cout << ans << endl;
	}

	return 0;
}

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

set<long long> sep;
long long ary[1010];
int main(){
	int N;
	long long M,hog,res;
	set<long long>::iterator it,it2;
	while(1){
		sep.clear();
		cin>>N>>M;
		if(N==0&&M==0) break;
		for(int i=0;i<N;i++){
			cin>>ary[i];
		}
		ary[N]=0;
		for(int i=0;i<=N;i++){
			for(int j=i;j<=N;j++){
				if(ary[i]+ary[j]<=M){
					sep.insert(ary[i]+ary[j]);
				}
			}
		}
		res=0;
		it2=sep.end();
		it2--;
		for(it=sep.begin();it!=sep.end();it++){
			while((*it)+(*it2)>M)it2--;
			res=max(res,(*it)+(*it2));
		}
		cout<<res<<endl;

	}
	return 0;
}

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

using namespace std;

int main() {
  
  int n, m;
  
  while(cin >> n >> m && (n|m)) {
    int data[1000];
    int Q[1000001];
    
    n++;
    data[0] = 0;
    for(int i=1; i<n; i++) {
      cin >> data[i];
    }
    
    int T = 0;
    
    for(int i=0; i<n; i++) {
      Q[T++] = data[i];
    }
    
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
	Q[T++] = data[i] + data[j];
      }
    }
    
    sort(Q, Q+T);

    // for(int i=0; i<T; i++) cout << Q[i] << endl;
    
    int ans = 0;
    
    for(int i=0; i<T; i++) {
      int M = m - Q[i];
      if(M < 0) break;
      
      int lb = 0, ub = T;
      
      while(ub - lb > 1) {
	int mid = (lb + ub) / 2;
	
	if(Q[mid] <= M) {
	  lb = mid;
	} else {
	  ub = mid;
	}
      }
      ans = max(ans, Q[i]+Q[lb]);
    }
    
    cout << ans << endl;
  }
  
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int N,M,P[1001];
vector<int> PP;
int main() {
	while(scanf("%d %d",&N,&M),N|M) {
		PP.clear();
		memset(P,0,sizeof(P));
		for(int i=1;i<=N;i++) scanf("%d",&P[i]);
		for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) {
			PP.push_back((P[i]+P[j])%M);
		}
		sort(PP.begin(),PP.end());
		PP.erase(unique(PP.begin(),PP.end()),PP.end());
		int ans=0;
		for(int Qi=0;Qi<PP.size();Qi++) {
			int lb=0,rb=PP.size()-1;
			while((rb-lb)>1) {
				int mid=(lb+rb)/2;
				if(PP[mid]<=(M-PP[Qi])) lb=mid;
				else rb=mid-1;
			}
			int Qj=PP[lb];
			if(PP[rb]<=(M-PP[Qi])) Qj=max(Qj,PP[rb]);
			ans=max(ans,PP[Qi]+Qj);
		}
		printf("%d\n",ans);
	}
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#define REP(i,a,n) for(int i = a;i < n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;
int in(){int x; scanf("%d",&x); return x;}
int main(void){
   int n,m;
   while(scanf("%d%d",&n,&m),n+m){
      vector<int>_point(0);
      vector<int>point(0);
      point.push_back(0);
      _point.push_back(0);
      rep(i,n){
         int x; scanf("%d",&x);
         _point.push_back(x);
         for(int j = _point.size()-1;j >= 0;j--){
            point.push_back(x+_point[j]);
         }
      }

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

      int ma = 0;

      for(int i = 0;point[i] <= m;i++){
         vector<int>::iterator idx = upper_bound(point.begin(),point.end(),m-point[i]);

         idx -= 1;
         int num = *idx;
         ma = max(ma,point[i]+num);
      }
      printf("%d\n",ma);
   }
   return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  int t[n];
  for(int i=0;i<n;i++){
    cin>>t[i];
  }
  int ans=0;
  int max=0;
  sort(t,t+n);
  for(int i=n-1;i>=0;){
    if(ans+t[i]>=m){
      i--;
      continue;
    }
    ans+=t[i];
    //cout<<"ans "<<ans<<endl;
  }
  cout<<ans<<endl;
  return 0;
}