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>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int v1[1001];
int v2[1000000];

int main(){
  int n,m;
  while(n = getInt(), m = getInt(), n+m){
    int cnt = 0;
    REP(i,n) v1[i] = getInt();
    v1[n++] = 0;
    REP(i,n) rep(j,i,n) {
      if(v1[i] + v1[j] <= m)
        v2[cnt++] = v1[i] + v1[j];
    }
    sort(v2, v2+cnt);

    int ans = 0;
    REP(i,cnt){
      int a = v2[i];
      int b = m - a;
      int *it = lower_bound(v2, v2+cnt, b);
      if(it != v2) --it;
      ans = max(ans, a + *it);
    }

    print(ans);
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
	int n,m;
	while(1){
		cin>>n>>m;
		if(n==0)break;
		int ans=0;
		vector<int> a,sum;
		a.push_back(0);
		for(int i=1;i<=n;i++){
			int s;
			cin>>s;
			a.push_back(s);
		}
		sort(a.begin(),a.end());
		for(int i=0;i<=n;i++){
			for(int j=i;j<=n;j++){
				if(a[i]+a[j]<=m)sum.push_back(a[i]+a[j]);
			}
		}
		sort(sum.begin(),sum.end());
		int suml=sum.size()-1;
		for(int i=0;i<=suml;i++){
			while(sum[i]+sum[suml]>m)suml--;
			if(ans<sum[i]+sum[suml])ans=sum[i]+sum[suml];
		}
		cout<<ans<<endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;

int main(){
  int N, M;
  while(cin >> N >> M, N){
    vector<int> P(N);
    for(int i=0;i<N;i++)cin >> P[i];
    vector<int> cand;
    for(int i=0;i<N;i++)for(int j=i;j<N;j++)if(P[i] + P[j] <= M){
      cand.push_back(P[i] + P[j]);
    }
    for(int x : P)if(x <= M)cand.push_back(x);
    sort(cand.begin(), cand.end());
    int j = cand.size() - 1;
    int res = 0;
    for(int i=0;i<cand.size();i++){
      while(j > 0 && cand[i] + cand[j] > M)--j;
      if(cand[i] + cand[j] <= M){
        res = max(res, cand[i] + cand[j]);
      }
    }
    cout << res << endl;
  }
  return 0;
}

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

#define MAX_N 1000
int P[MAX_N + 1];
int N, M;
int table[5][MAX_N + 1];

int dfs(int a, int b, int c){
	if(c > M) return P[b] * -1;
	if(a == 0) return P[b];
	if(table[a][b] >= 0) return table[a][b];
	
	int cho = b;
	while(cho <= N){
		int d = dfs(a - 1, cho, c + P[b]) + P[b];
		table[a][b] = max(table[a][b], (d > M)? 0 : d);
		cho++;
	}
	
	return table[a][b];
}

int main(){
	while(1){
		for(int i = 0; i < 5; i++){
			for(int j = 0; j <= MAX_N; j++){
				table[i][j] = -1;
			}
		}
		
		scanf("%d%d", &N, &M);
		
		if(N == 0 && M == 0) return 0;
		
		for(int i = 1; i <= N; i++) scanf("%d", &P[i]);
		
		printf("%d\n", dfs(4, 0, 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<q*q;i++){
		r=m-x[i];
		//cout<<r<<"を探します"<<endl;
		s=0;
		e=q*q;
		while(1){
			l=(s+e)/2;
			//cout<<"l="<<l<<endl;
			if(x[l]>r){
				e=l-1;
			}else if(x[l]<r){
				s=l+1;
			}
			if(s>=e || x[l]==r){
				if(x[i]+x[l]<=m)
				ans=max(ans,x[i]+x[l]);
				break;
			}
		}
	}
		cout<<ans<<endl;
	}
	return 0;
}

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

int main(){
    while(1){
        lastans=0;
        po.clear();
        //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);
        //lastans=po[n-1];

        for(int i=4;i>0;i--){
            for(int j=n-1;j>=0;j--){
                if(m>=i*po[j]){
                    lastans=max(i*po[j],lastans);

                    for(int k=4-i;i>0;i--){
                        for(int s=0;s<n;s++){
                            if(m>=i*po[j]+po[s]){
                                lastans=max(i*po[j]+po[s],lastans);

                                for(int t=4-k;t>0;t--){
                                    for(int u=0;u<n;u++){
                                        if(m>=i*po[j]+po[s]+po[u]){
                                         lastans=max(i*po[j]+po[s]+po[u],lastans);

                                         for(int v=4-t;v>0;v--){
                                            for(int w=u-1;w>=0;w--){
                                                if(m>=i*po[j]+po[s]+po[u]+po[w]){
                                                    lastans=max(lastans,i*po[j]+po[s]+po[u]+po[w]);
                                                    //break;
                                                }else break;
                                            }
                                         }
                                            //break;
                                        }else break;
                                    }
                                }
                                //break;
                            }else break;

                        }
                    }

                    //lastans=max(lastans,po[j]*i);
                    //break;
                }else break;
            }
        }



        cout << lastans << endl;
        //cout <<"ss=" << ss << endl;
    }

    return 0;
}