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;
vector<long int> po;
vector <pair <int ,long int> > sss;
long int n,a,low,m,lastans;
map <int, int> mp;
void solve(int dp,long int sum);

int main(){
    while(1){
        lastans=0;
        po.clear();
        mp.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(mp[i*po[j]]>0);
                else{
                if(m>=i*po[j]){
                    lastans=max(i*po[j],lastans);
                    mp[i*po[j]]++;

                    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);
                                mp[po[s]]++;

                                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);
                                         mp[po[u]]++;

                                         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]);
                                                    mp[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;
}

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

long long int solve(long long int a, vector<long long int>& side_b, long long int m){
  int left = 0, right = side_b.size() - 1;
  while(right - left > 1){
    int middle = (right + left) / 2;
    if(side_b[middle] + a <= m){ left = middle; }
    else{ right = middle; }
  }
  return a + side_b[left];
}


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

    vector<long long int> input(n);
    for(int i = 0; i < n; i++){ cin >> input[i]; }

    vector<long long int> side_a;
    for(int i = 0; i < n; i++){
      side_a.push_back(input[i]);
      for(int j = 0; j < n; j++){
        side_a.push_back(input[i] + input[j]);
      }
    }
    sort(side_a.begin(), side_a.end());
    side_a.push_back(side_a[side_a.size() - 1]);

    long long int ans = 0;
    for(size_t i = 0; i < side_a.size(); i++){
      long long int a = side_a[i];
      long long int tmp = solve(a, side_a, m);
      if(tmp <= m){ ans = max(ans, tmp); }
    }
    
    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

#define MAX(a,b) ((a)>(b)?(a):(b))

int main()
{
    int N;
    long long M;

    cin >> N >> M;

    while(N!=0 && M!=0) {
        vector<int> p(N);
        set<long long> st[2];

        long long ans=0;

        for(int i=0; i<N; ++i) {
            cin >> p[i];
        }

        sort(p.begin(),p.begin()+N);

        for(int i=0; i<N; ++i) {
            if(p[i]<M) st[0].insert(p[i]);
            ans=MAX(ans,p[i]);
        }

        for(int k=1; k<4; ++k) {
            for(auto itr=st[(k-1)%2].begin(); itr!=st[(k-1)%2].end(); ++itr) {
                auto p_itr=upper_bound(p.begin(),p.end(),M-*itr);
                if(p_itr==p.begin()) {
                    st[(k-1)%2].clear();
                    break;
                } else {
                    --p_itr;
                }
                ans=MAX(ans,*itr+*p_itr);
                st[k%2].insert(*itr+*p_itr);
            }
        }

        cout << ans << endl;


        cin >> N >> M;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int N;
	ll M;
	cin >> N >> M;
	if (N == 0 && M == 0) return 0;
	vector<ll> P(N + 1);
	REP(i, N) cin >> P[i];
	P[N] = 0;
	vector<ll> sum((N + 1) * (N + 1));
	REP(i, N + 1)REP(j, N + 1) sum[(N + 1) * i + j] = P[i] + P[j];
	sort(sum.begin(),sum.end());
	ll res = 0;
	REP(i, N + 1)REP(j, N + 1) {
		if (sum[(N + 1) * i + j] <= M) {
			res = max(res,sum[(N + 1) * i + j]);
			int n = upper_bound(sum.begin(), sum.end(), M - sum[(N + 1) * i + j]) - sum.begin() - 1;
			if (n >= 0) res = max(res, sum[(N + 1) * i + j] + sum[n]);
		}
	}
	cout << res << "\n";

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 1002

int p[MAX], dp[MAX];
int main(){
	int n, m, ans;
	while(cin >> n >> m, n){
		fill(dp, dp+MAX, 0);
		for(int i = 0;i < n;i++)cin >> p[i];
		ans = 0;
		
		for(int i = 0;i < 3;i++){
			dp[0] = 5;
			for(int j = p[i];j <= m;j++){
				dp[j] = max(dp[j], dp[j-p[i]]-1);
				//if(dp[j])ans = max(ans, j);
				if(dp[j] && ans < j)ans = j;
			}
		}
		//for(int i = 1;i <= 50;i++)cout << dp[i] << endl;
		cout << ans << endl;
	}
	return 0;
}