Go: ///
// File:  0529.go
// Author: ymiyamoto
//
// Created on Wed Feb 28 20:39:28 2018
//
package main

import (
	"fmt"
	"sort"
)

func max(n, m int) int {
	if n < m {
		return m
	} else {
		return n
	}
}

func main() {
	for {
		var N, M int
		fmt.Scan(&N, &M)
		if N == 0 && M == 0 {
			break
		}

		arr := make([]int, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Ints(arr)

		sums := make([]int, 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.Ints(sums)

		m := 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 <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#define fi "darts.inp"
#define fo "darts.out"
using namespace std;
long long n,m,a[100011],ans;
vector<long long > vt;
int main()
{
//    freopen (fi , "r" ,stdin);
//    freopen (fo , "w" ,stdout);
    while (1)
    {
    scanf("%lld%lld",&n,&m);
    if (n==0&&m==0) return 0;
    for (int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    n++;
    a[n]=0;
    vt.clear();
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            vt.push_back(a[i]+a[j]);
        }
    sort(vt.begin(),vt.end());
    int c;
    c=vt.size()-1;
    for (int i=0;i<vt.size();i++)
    {
        while (c>=0&&vt[c]+vt[i]>m)
            c--;
        if (vt[c]+vt[i]<=m) ans=max(ans,vt[c]+vt[i]);
    }
    cout<<ans<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589


#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*) array, (T*) (array + N), val);
}
pii Dir[8] = { //?§????
	{ 0 ,1 },{ -1 ,0 },{ 1 ,0 },{ 0 ,-1 },
	{ 1 ,1 },{ 1 ,-1 },{ -1 ,1 },{ -1 ,-1 }
};

signed main() {
	int n, m;

	scanf("%d %d", &n, &m);
	vector<int> po(n);
	REP(i, n) {
		scnaf("%d", &po[i]);
	}
	po.push_back(0);
	set<int> toku;
	REP(i, n+1) {
		REP(j, n+1) {
			toku.insert(po[i]+po[j]);
		}
	}
	int ans = 0;
	for (auto itr : toku) {
		auto itr2 = --toku.upper_bound(m-itr);
		if(itr2 != toku.end())
			ans = max(itr+*itr2, ans);
	}
	printf("%d\n",ans);
	return 0;
}

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

int N,M;
int P[1001];
int S;

int arara(){
    for(int i = 0; i <= N; i++){
        for(int j = 0; j <= N; j++){
            for(int k = 0; k <= N; k++){
                for(int l = 0; l <= N; l++){
                    if(S <= P[i] + P[j] + P[k] + P[l]&&P[i] + P[j] + P[k] + P[l] <= M){
                        S = P[i] + P[j] + P[k] + P[l];
                    }
                }
            }
        }
    }
    return S;
}

int main(){
    P[0] = 0;
    for(;;){
        if(cin >> N >> M){
            for(int i = 1; i < N + 1; i++){
                cin >> P[i];
            }
            cout << arara() << endl;
        }
    }
    return 0;
}

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

#define ef(e, a, b) ((e) ? (a) : (b))

using namespace std;

int main ( void )
{
	int n, m;
	int p[4][1000];

	while ( cin >> n >> m, (n | m) )
	{
		int ans = 0;
		
		for (int i = 0; i < n; ++i)
		{
			cin >> p[0][i];
			
			ans = max(ans, ef((p[0][i]*4 == m) || (p[0][i]*3 == m) || (p[0][i]*2 == m), m, ef(p[0][i] > m, 0, p[0][i])));
			
			for (int j = 1; j < 4; ++j) { p[j][i] = 0; }
		}
		
		for (int i = 1; i < 4; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				for (int k = 0; k < n; ++k)
				{
					int t = p[i-1][j] + p[0][k];
					p[i][j] = max(p[i][j], ef(t > m, 0, t));
					ans = max(ans, p[i][j]);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
typedef long long L;
using namespace  std;
int main(){
	int n;
	L m,p[1001],sum;
	vector<L> sum1;
	vector<L>::iterator it,ele;
	scanf("%d %lld",&n,&m);
	while(n!=0&&m!=0){
		sum=0;
		for(int i=0;i<n;i++){
			scanf("%lld",&p[i]);
		}
		p[n]=0;
		for(int i=0;i<=n;i++){
			for(int i2=0;i2<=n;i2++){
				sum1.push_back(p[i]+p[i2]);
			}
		}
		sort(sum1.begin(),sum1.end());
		for(it=sum1.begin();it<sum1.end();it++){
			if(*it>m)
				break;
			ele=upper_bound(sum1.begin(),sum1.end(),(m-(*it) ) );
			if(ele!=sum1.begin()){
				ele--;
			}
			if(sum<=(*it)+(*ele)){
				sum=(*it)+(*ele);
			}
			if(sum==m)
				break;
		}
		printf("%lld\n",sum);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 1005
#define LSIZE 1100005
#define INF 1000000005LL

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

int n;
ll M;
ll T[SIZE];
ll S[LSIZE];

ll solve()
{
	scanf("%d %lld",&n,&M);
	if(n+M ==0)return -1;
	for(int i=1;i<=n;i++)scanf("%lld",&T[i]);
	T[0]=0;
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			S[i*(n+1)+j] = T[i]+T[j];
		}
	}
	ll maxv=0;
	ll v;
	sort(S,S+(n+1)*(n+1));
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			if(T[i]+T[j]<=M){
				v = T[i]+T[j]+S[upper_bound(S,S+(n+1)*(n+1),M-T[i]-T[j])-S-1];
				maxv = max(maxv,v);
			}
		}
	}
	return maxv;
}

int main()
{
	while(1){
		ll ans = solve();
		if(ans==-1)break;
		else printf("%lld\n",ans);
	}
	return 0;
}

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

int p[MAX], q[MAX*MAX];
int main(){
	int n, m, ans, qq;
	while(cin >> n >> m, n){
		for(int i = 1;i <= n;i++)scanf("%d", &p[i]);
		ans = 0;p[0] = 0;qq = 0;
		
		for(int i = 0;i <= n;i++)
			for(int j = 0;j <= n;j++)
				if(p[i]+p[j] <= m)q[qq++] = p[i] + p[j];
		
		sort(q, q+qq);
		int t;
		for(int i = 0;i < qq;i++){
			t = upper_bound(q, q+qq, (m-q[i]))-q;
			if(t-1 >= 0)ans = max(ans, q[i]+q[t-1]);
		}
		cout << ans << endl;
	}
	return 0;
}

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

vector<long long int> p;
vector<long long int> p2;

int main(void){
	int a,b,n,m;
	while(1){
		long long int ans=0;
		scanf("%d%d",&n,&m);
		if(n==0 && m==0)break;
		p.clear();
		p2.clear();
		for(a=0;a<n;a++){
			int num;
			scanf("%d",&num);
			p.push_back(num);
		}
		sort(p.begin(),p.end());
		p2.push_back(0);
		for(a=0;a<n;a++){
			p2.push_back(p[a]);
			for(b=0;b<n;b++){
				p2.push_back(p[a]+p[b]);
			}
		}
		p2.push_back(0);
		sort(p2.begin(),p2.end());
		for(a=0;a<p2.size();a++){
			long long int res=m-p2[a];
			if(m-p2[a]<0)continue;
			long long int pp=*(lower_bound(p2.begin(),p2.end(),res)-1);
			if(ans<pp+p2[a])ans=pp+p2[a];
			//cout << pp+p2[a] << endl;
		}
		printf("%d\n",ans);
	}
	return 0;
}