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<cstring>
using namespace std;

int n, m;
int p[1001], dp[1001][5];
int main()
{
  while(true){
    //chap1
    cin >> n >> m;
    if(!n && !m) break;
    
    memset(dp, 0, sizeof dp);
    for(int i = 1; i <= n; i++)
      cin >> p[i];

    //chap2
    for(int i = 1; i <= n; i++)
      for(int j = 0; j <= 4; j++)
	for(int k = 0; j + k <= 4; k++)
	  if(dp[i - 1][j] + p[i] * k <= m)
	    dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] + p[i] * k);

    cout << dp[n][4] << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a,b) (abs((a)-(b))<EPS)
int p[1001];
int ans;
vector<int> ab,cd;
int n,m;
int main()
{
	while(1)
	{
		memset(p,0,sizeof(p));
		ab.clear();cd.clear();
		cin >> n >> m;
		ans=0;
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++)
		{
			cin >> p[i];
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				ab.pb(p[i]+p[j]);
				cd.pb(m-(p[i]+p[j]));
			}
		}
		sort(cd.begin(),cd.end());
		for(int i=0;i<sz(ab);i++)
		{
			vector<int>::iterator it=lower_bound(cd.begin(),cd.end(),ab[i]);
			ans=max((*it)*-1+m+ab[i],ans);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>

using namespace std;

struct Info{
	Info(){
		number = 0;
	}
	int number;
};

void func(int N,int M){
	Info info[N];

	for(int i = 0; i < N; i++){
		scanf("%d",&info[i].number);
	}

	for(int i = 1; i <= N-1; i++){
		for(int k = N-1; k >= i; k--){
			if(info[k].number < info[k-1].number)swap(info[k],info[k-1]);
		}
	}

	int sum = 0,left,right,m,add;

	for(int loop = 0; loop < 4; loop++){
		left = 0; right = N-1,add=0;
		m = (left+right)/2;
		while(left <= right){
			if(sum + info[m].number <= M){ //????????§????????????????¶??????????M????¶????????????°?????¢???
				add = info[m].number;
				left = m+1;
			}else{
				right = m-1;
			}
			m = (left+right)/2;
		}
		sum += add;
	}

	printf("%d\n",sum);

}

int main(){

	int N,M;

	while(true){
		scanf("%d %d",&N,&M);
		if(N == 0 && M == 0)break;

		func(N,M);
	}

	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 1000005
#define INF 1000000005LL

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

int n;
ll M;
ll T[SIZE];
set<ll> S;
ll K;

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;
	S.clear();
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			S.insert(T[i]+T[j]);
		}
	}
	ll maxv=0;
	set<ll>::iterator ii;
	for(set<ll>::iterator it=S.begin();it!=S.end();it++){
		if((*it)<=M){
			ii = upper_bound(S.begin(),S.end(),M-(*it));
			ii--;
			maxv = max(maxv,(*it)+(*ii));
		}
	}
	return maxv;
}

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

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

#define ll int

void solve(){
    ll n,m;
    ll ans;

    while(cin>>n>>m,n|m){
        vector<ll> v,sum_v;

        for(ll i=0;i<n;i++){
            ll t;
            cin >> t;
            v.push_back(t);
        }

        v.push_back(0);

        for( auto t:v ){
            for( auto s:v ){
                sum_v.push_back(t+s);
            }
        }

        sort(sum_v.begin(),sum_v.end());

        ans=-1;
        for( auto t:sum_v ){
            if( binary_search(sum_v.begin(),sum_v.end(),m-t) ){
                ans=m;
                break;
            }else{
                auto it=upper_bound(sum_v.begin(),sum_v.end(),m-t);
                if( it != sum_v.begin() ){
                    ans=max(ans,t + *(--it));
                }
            }
        }

        cout<<ans<<endl;
    }
}

int main(){
    solve();
    return 0;
}

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

typedef long long ll;
int main() {
    while(true) {
        ll n, m;
        cin >> n >> m;
        if(!n && !m) break;
        vector<ll> v(n+1, 0);
        vector<ll> v2;
        for(int i=1;i<n+1;i++) cin >> v[i];
        for(int i=0;i<n+1;i++) {
            for(int j=i;j<n+1;j++) {
                v2.push_back(v[i]+v[j]);
            }
        }

        sort(v2.begin(), v2.end());
        ll ma = 0;
        for(int i=0;i<v2.size();i++) {
            vector<ll>::iterator itr = upper_bound(v2.begin(), v2.end(), m-v2[i]);
            int d = distance(v2.begin(), itr);
            if(d > 0) {
                ma = max<ll>(ma, v2[i]+v2[d-1]);
            }
        }
        cout << ma << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(true){
		cin>>n>>m;
		if(n==0)	return 0;
		vector<int> p(n);
		for(int i=0;i<n;i++)	cin>>p[i];
		p.push_back(0);	n++;
		set<int> s;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++)	if(p[i]+p[j]<m)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(auto it=s.begin();it!=s.end();it++){
			auto it2=s.lower_bound(*it);
			if(it2!=s.begin())	it2--;
			if(*it+*it2<=m)	ans=max(ans,*it+*it2);
			else 	break;
		}
		cout<<ans<<endl;
	}
}

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

using namespace std;

int main()
{
	for(int n,m;cin>>n>>m,n|m;){
		vector<int> p(n);
		for(int i=0;i<n;i++)
			cin>>p[i];
		set<int> a;
		a.insert(0);
		for(int i=0;i<n;i++){
			a.insert(p[i]);
			for(int j=0;j<n;j++)
				a.insert(p[i]+p[j]);
		}
		int res=0;
		for(set<int>::iterator i=a.begin(),j;i!=a.end();++i){
			j=a.upper_bound(m-*i);
			if(j!=a.begin()){
				--j;
				res=max(res,*i+*j);
			}
		}
		cout<<res<<endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int n, m, arr[1001], sum[1000001];
	while (cin >> n >> m){
		if (!n && !m)return 0;
		int cnt = 0, res = 0;
		for (int i = 1; i <= n; ++i)scanf("%d", &arr[i]);
		for (int i = 0; i <= n; ++i){
			for (int j = n; j > i; --j){
				if (arr[i] + arr[j] <= m)sum[cnt++] = arr[i] + arr[j];
			}
		}
		sort(sum, sum + cnt);
		for (int i = 0; i < cnt; ++i){
			res = max(res, sum[i] + *(upper_bound(sum, sum + cnt, m - sum[i]) - 1));
		}
		printf("%d\n", res);
	}
}