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 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;
int n,m,p[101];
vector<int>v;
int nibuntansaku(int x){
  int l=0,r=v.size();
  while(l<r){
    int mid=(l+r)/2;
    if(v[mid]>x)r=mid;
    else l=mid+1;
  }
  if(v[l]>x&&l!=0)l--;
  return v[l];
}
int main(){
  while(cin>>n>>m,n){
    v.clear();
    v.push_back(0);
    for(int i=0;i<n;i++){
      cin>>p[i];
      v.push_back(p[i]);
    }
    for(int i=0;i<n;i++)
      for(int j=i+1;j<n;j++)
        v.push_back(p[i]+p[j]);
    sort(v.begin(),v.end());
    int sum=0;
    for(int i=0;i<v.size();i++){
      int x=nibuntansaku(m-v[i])+v[i];
      if(x<=m)sum=max(x,sum);
    }
    cout<<sum<<endl;
  }
}

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


sort(s1,s1+g);
rep(i,g)s2[i]=s1[i];
//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<25;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 <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){
	long long int s=0;
		vector<long long int> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
			}
			else
				n--;
		}
		cout<<s<<endl;
	}
	
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
#define MOD 1000000007
#define INF 1<<30
#define LINF (ll)1<<62
#define MAX 510000
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x) (x).begin(),(x).end()
#define uni(q) unique(all(q)),q.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main(){
    while(1){
        ll n,m; cin >> n >> m;
        if(n==0 && m==0) break;
        vl a(n);
        vl sums;
        sums.push_back(0);
        rep(i,n) {
            cin >> a[i];
            sums.push_back(a[i]);
        }
        rep(i,n){
            REP(j,i,n){
                sums.push_back(a[i]+a[j]);
            }
        }
        sort(all(sums));
        ll ans = 0;
        for(auto i : sums){
            auto itr = upper_bound(all(sums), m-i);
            if(itr == sums.begin()) continue;
            itr--;
            ans = max(ans, i+*itr);
        }
        cout << ans << endl;
        sums.clear();
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int dp[5][1005];

int main()
{
//	cut here before submit 
//	freopen ("testcase.darts", "r", stdin );
	int n, m;
	while (cin >> n >> m && n && m ){
		memset (dp, 0, sizeof (dp ) );
		vector<int> p (n, 0 );
		rep (i, n ){
			cin >> p[i];
		} // end rep
//		sort (ALL (p ) );

		rep (i, n ){
			dp[0][i] = (p[i] <= m ? p[i] : 0 );
		} // end rep
		rep (i, 4 ){
			rep (j, n ){
				if (dp[i][j] != 0 ){
					rep (k, n ){
						dp[i+1][k] = (p[k] + dp[i][j] <= m ? max (dp[i+1][k] , p[k] + dp[i][j] ) : dp[i+1][k] );
					} // end rep
				} // end if
			} // end rep
		} // end rep
/*
		rep (i, 4 ){
			rep (j, n ){
				cout << setw(3) << dp[i][j];
			} // end rep
			cout << endl;
		} // end rep
*/
		int res = 0;
		rep (i, 4 ){
			for (int j = m; j >= 0; j-- ){
				if (dp[i][j] != 0 ){
					res = max (res, dp[i][j] );
					break;
				} // end if
			} // end for
		} // end rep
		cout << res << endl;

	} // end loop
		
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
vector<long int> po;
long int n,a,low,m,lastans,memo[1005],ss;
void solve(int dp,long int sum);

int main(){
    while(1){
        ss=0;
        lastans=0;
        po.clear();
        low=100000000;
        for(int i=0;i<1005;i++)memo[i]=0;

        cin >> n >> m;
        if(n==0&&m==0)break;
        for(int i=0;i<n;i++){
            cin >> a;
            po.push_back(a);
            low=min(low,a);
        }
        sort(po.begin(),po.end());
        cout << po[0] << endl;
        solve(0,0);
        cout << lastans << endl;
        //cout <<"ss=" << ss << endl;
    }

    return 0;
}

void solve(int dp,long int sum){
    //long int ans=0;
    //ss++;
    //cout <<"ss=" << ss << endl;
    if(dp==4)return ;
    else{
    // memo[dp]=max(sum,memo[dp]);

     for(int i=0;i<n;i++){
        if(sum+po[i]<m){
            lastans=max(lastans,sum+po[i]);
            //ans=max(ans,sum+po[i]);
            solve(dp+1,sum+po[i]);
            }else break;
        }
    }
    return;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	while(1){
		int i,j,n,m,s=0,o=1;
		static int p[1001]={0},up[1001*1001]={0};
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)return 0;
		up[0]=0;
		for(i=1;i<=n;i++){
			scanf("%d",&p[i]);
			for(j=i;j>=0;j--){
				up[o]=p[i]+p[j];
				o++;
			}
		}
		sort(up,up+o);
		for(i=0;i<o;i++){
			for(j=i;up[i]+up[j]<m&&j<o;j++);
			if(up[i]+up[j-1]<=m)s=max(s,up[i]+up[j-1]);
		}
		printf("%d\n",s);
	}
	return 0;
}