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 <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];
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<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j,k,l;
  int a[1000],m,n,p;
  while(cin>>n>>m&&n+m){
    for(i=0;i<n;i++)
      cin>>a[i];
    sort(a,a+n,greater<int>());
    p=0;
    for(i=0;i<n&&a[i]>m;i++);
    if(i!=n)
      p=a[i];
    for(j=n-1;j>=i;j--){
      if(a[i]+a[j]>m){
	i++;
	j++;
      }else{

	for(k=n-1;k>=j;k--){
	  if(a[i]+a[j]+a[k]>m)
	    break;
	  else{

	    for(l=n-1;l>=k;l--){
	      if(a[i]+a[j]+a[k]+a[l]>m)
		break;
	      else
		p=max(p,a[i]+a[j]+a[k]+a[l]);
	    }

	    p=max(p,a[i]+a[j]+a[k]);
	  }
	}

	p=max(p,a[i]+a[j]);
      }
    }
    cout<<p<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <set>
#define N_MAX 1001
using namespace std;

int N, M;
int P[N_MAX];
set<int> sp;

int solve() {
  set<int> s;
  for(int i = 0; i <= N; i++) {
    for(int j = i; j <= N; j++) {
      for(int k = j; k <= N; k++) {
        int tmp = P[i] + P[j] + P[k];
        if(tmp > M) tmp = 0;
        if(tmp == 0) continue;
        s.insert(tmp);
      }
    }
  }

  for(int i = 0; ; i++) {
    for(set<int>::iterator it = s.begin(); it != s.end(); it++) {
      int tmp = M - *it - i;
      if(sp.find(tmp) != sp.end()) return tmp + *it;
    }
  }
}

int main() {
  while((cin >> N >> M) && N && M) {
    P[0] = 0;
    sp.clear();
    sp.insert(0);
    for(int i = 1; i <= N; i++) {
      cin >> P[i];
      sp.insert(P[i]);
    }
    cout << solve() << endl;
  }
  return 0;
}

C++: #include <cstdio>

int main()
{
while(true) {
int n, m;
vector<int> x, xxx;
scanf("%d%d", &n, &m);
if(n == 0)
break;
x.resize(i);
for(int i = 0; i < n; ++i)
scanf("%d", &x[i]);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j)
xxx.push_back(x[i] + x[j]);
}
xxx.push_back(0);
sort(xxx.begin(), xxx.end());
int ans = 0;
for(int i = 0; i < xxx.size(); ++i) {
int r = m - xxx[i];
if(r < 0)
continue;
int idx = upper_bound(xxx.begin(), xxx.end(), r) - xxx.begin() - 1;
if(idx < 0)
continue;
ans = max(ans, r + xxx[idx]);
}
printf("%d\n", ans);
}
return 0;
}

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

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> plli;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>=0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()

const ll mod = 1e9 + 7;
const ll INF = 1<<30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n, m;

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    while (n != 0) {
        int p[1005] = {};
        rep(i, n) cin >> p[i];
        vector<int> score;
        score.push_back(0);
        rep(i, n) score.push_back(p[i]);
        rep(i, n) rep(j, n) {
            score.push_back(p[i] + p[j]);
        }
        sort(all(score));
        score.erase(unique(all(score)), score.end());
        int ans = 0;
        rep(i, score.size()) {
            if (score[i] > m) break;
            int l = 0;
            int r = score.size() + 1;
            while (r - l > 1) {
                int mid = (r + l) / 2;
                if (score[i] + score[mid] <= m) l = mid;
                else r = mid;
            }
            int idx = l;
            if (score[i] + score[idx] > m) continue;
            ans = max(ans, score[i] + score[idx]);
        }
        cout << ans << endl;
        cin >> n >> m;
    }
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

int main() {
	int n, m;
	cin >> n >> m;
	vector<int> point(n);
	REP(i, n){
		cin >> point[i];
	}
	point.push_back(0);
	vector<int> sum = point;
	REP(i, n + 1){
		REP(j, n + 1){
			if (count(sum.begin(), sum.end(), sum[i] + sum[j]) == 0)
				sum.push_back(sum[i] + sum[j]);
		}
	}
		SORT(sum);
		int ma = 0;
		REP(i, sum.size()){
			int left = 0;
			int right = sum.size() - 1;
			while (left < right){
				if (sum[left + (right - left) / 2] + sum[i] <= m){
					ma = max(ma, sum[left + (right - left) / 2] + sum[i]);
					left = left + (right - left) / 2 + 1;

				}
				else{
					right = left + (right - left) / 2 - 1;
				}
			}
		}
		cout << ma << endl;
	}