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++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n, m, ans;

void solve(vector<int>& p){
	vector<int> q;
	
	sort( p.begin() , p.end() );
	p.erase( unique(p.begin(),p.end()) , p.end() );
	for(int i=0 ; i < p.size() ; i++ ){
		if( p[i] > m ) break;
		
		ans = max( ans , p[i] );
		for(int j=0 ; j < p.size() ; j++ ){
			if( p[i]+p[j] > m ) break;
			
			q.push_back( p[i]+p[j] );
			ans = max( ans , p[i]+p[j] );
		}
	}
	
	sort( q.begin() , q.end() );
	q.erase( unique(q.begin(),q.end()) , q.end() );
	
	for(int i=0 ; i < q.size() ; i++ ){
		int idx = lower_bound( q.begin() , q.end() , m-q[i] ) - q.begin() - 1;
		if( idx >= 0 && idx < q.size() ){
			ans = max( ans , q[i] + q[idx] );
		}
		
		idx = lower_bound( p.begin() , p.end() , m-q[i] ) - p.begin() - 1;
		if( idx >= 0 && idx < p.size() ){
			ans = max( ans , q[i] + p[idx] );
		}
	}
}

int main(){
	while( cin >> n >> m , n || m ){
		vector<int> p;
		for(int i=0 ; i < n ; i++ ){
			int e;
			cin >> e;
			p.push_back( e );
		}
		ans = 0;
		solve( p );
		cout << ans << endl;
	}
}

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

	int tsP[1001 * 1001];
	int main() {
		int N, M;
		int P[1001];
		int ans;
		int cnt;

		P[0] = 0;

		//cin >> N >> M;
		while (cin>>N>>M,N&&M) {
			ans = 0;
			cnt = 0;
			for (int i = 1; i <= N; i++) {
				cin >> P[i];
			}

			for (int i = 0; i <= N; i++) {
				for (int j = i; j <= N; j++) {
					if (P[i] + P[j] <= M) {
						tsP[cnt++] = P[i] + P[j];
					}

				}
			}
			sort(tsP, tsP + cnt);
			for (int i = 0; i < cnt; i++) {
				for (int j = i; j < cnt; j++) {
					if (tsP[i] + tsP[j] <= M) {
						ans = max(ans, tsP[i] + tsP[j]);
					}
				}
			}
			cout << ans << endl;
		}
		return 0;
	}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int N,M;
int P[1001];
int num[600000];
int main()
{
	while(true){
		scanf("%d%d",&N,&M);
		if(N==0&&M==0)return 0;
		for(int i=0;i<N;i++)scanf("%d",&P[i]);
		N++;
		int now1=0;
		for(int i=0;i<N;i++){
			for(int j=i;j<N;j++){
				num[now1]=P[i]+P[j];
				now1++;
			}
		}
		sort(num,num+now1);
		int ans=0;
		int I=0;int J=now1-1;
		while(I<now1&&J>=0){
			if(num[I]+num[J]<=M){
				ans=max(ans,num[I]+num[J]);
				I++;
			}
			else J--;
		}
		printf("%d\n",ans);
	}
}

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

int main(){
	int n;
	ll m;
	while( cin >> n >> m, n || m ){
		vector<ll> vp(n+1);
		vp[0] = 0;
		for( int i = 0; i < n; i++ ){
			cin >> vp[i+1];
		}
		vector<ll> points;
		for( int i = 0; i <= n; i++ ){
			for( int j = i; j <= n; j++ ){
				points.push_back(vp[i]+vp[j]);
			}
		}
		sort(points.begin(), points.end());

		ll ans = 0;
		for( int i = 1; i < points.size(); i++ ){
			if( m < points[i] || points[i] == points[i-1] ) continue;
			ll diff = m - points[i];
			int left = 0, right = points.size()-1;
			while( 1 ){
				ll next = (left + right) / 2;
				if( next == left || next == right ){
					if( points[next] <= diff ) ans = max(ans, points[i] + points[next] );
					break;
				}
				if( points[next] == diff ){
					ans = points[i] + points[next];
					break;
				}else if( points[next] < diff ){
					left = next;
				}else{
					right = next;
				}
			}
		}
		cout << ans << endl;
	}
}

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

using namespace std;

int main() {
    while(true) {
        int N, M;
        cin >> N >> M;
        if(!N && !M) break;

        vector<int> points;
        vector<int> points_of_two;
        for(int i = 0; i < N; ++i) {
            int p;
            cin >> p;
            points.push_back(p);
        }
        for(int i = 0; i < N; ++i) {
            if(points[i] <= M) points_of_two.push_back(points[i]);
            for(int j = 0; j < i; ++j) {
                int sum = points[i] + points[j];
                if(sum <= M) points_of_two.push_back(sum);
            }
        }
        points_of_two.push_back(0);
        sort(points_of_two.begin(), points_of_two.end());
        points_of_two.erase(unique(points_of_two.begin(), points_of_two.end()), points_of_two.end());

        int ans = 0;
        for(int i = 0; i < points_of_two.size(); ++i) {
            int rem = M - points_of_two[i];
            int left = 0, right = points_of_two.size();
            while(left < right) {
                int center = (left+right)/2;
                if(points_of_two[center] <= rem) {
                    left = center+1;
                }
                else {
                    right = center;
                }
            }
            if(left != 0) --left;
            int score = points_of_two[i] + points_of_two[left];
            if(score > ans) ans = score;
        }
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

char dp[200000000];

int main() {
  for (;;) {
    int N, M;
    cin >> N >> M;
    if (!(N || M)) return 0;
    fill(dp, dp+M+1, 5);
    dp[0] = 0;
    for (int i = 0; i < N; i++) {
      int p;
      cin >> p;
      for (int j = p; j <= M; j++)
        if (dp[j-p] < 4 && dp[j-p]+1 < dp[j])
          dp[j] = dp[j-p] + 1;
    }
    for (int i = M; i >= 0; i--)
      if (dp[i] <= 4) {
        cout << i << endl;
        break;
      }
  }
}

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

using namespace std;

int main( void )
{
	int n, m, input[1001];
	while(cin >> n >> m, n | m) {
		for(int i=0; i < n; ++i) cin >> input[i];
		sort(input, input+n, greater<int>());
		//for(int i=0; i < n; ++i) cout << input[i] << ' ';
		//cout << endl;
		input[n++] = 0;
		int Max = 0;
		for(int i=0; i < n; ++i)
			for(int j=i; j < n; ++j)
				for(int k=j; k < n; ++k)
					for(int l=k; l < n; ++l) {
						int sum = input[i] + input[j] + input[k] + input[l];
						if(sum <= m && Max < sum) Max = sum;
					}
		cout << Max << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h> 
using namespace std;
using ll=long long;
typedef pair<int,int> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
set<string> c;
const ll mod=1000000007;
const ll mod2=998244353;
const ll inf=100000000000000000;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}

int main(){
while(1){
ll n,m;
cin>>n>>m;
if(n==0&&m==0)return 0;
vector<ll> a(n+1),b;
for(int i=0;i<n;i++)cin>>a[i];
a.push_back(0);
for(int i=0;i<n+1;i++){
    for(int j=0;j<n+1;j++){
        b.push_back(a[i]+a[j]);
    }
}
ll ans=0;
sort(all(b));
for(int i=0;i<n*n;i++){
     ll tmp=m-b[i];
  // cout<<b[i]<<endl;
     int ite=upper_bound(all(b),tmp)-b.begin();
     if(ite==0||ite==n*n||b[i]+b[ite-1]>m)continue;
  //  cout<< b[i]+b[ite-1]<<endl;
     ans=max(ans,b[i]+b[ite-1]);
}
cout<<ans<<endl;
}
}