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 main(){
while(1){
	int n,m,a,ans=0;
	vector<int> p,pp;
	scanf("%d%d",&n,&m);
	p.push_back(0);
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		p.push_back(a);
	}
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			pp.push_back(p[i]+p[j]);
		}
	}
	sort(pp.begin(),pp.end());
	int s=pp.size()-1;
	for(int i=0;i<=s;i++){
		while(pp[i]+pp[s]>m)s--;
		if(ans<pp[i]+pp[s])ans=pp[i]+pp[s];
	}
	printf("%d\n",ans);
}
}

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

const long long INF = 200000000;
int N;
long long M;
vector<long long> target;

int main(void) {
	while (cin >> N >> M, M) {
		// input
		target.clear(); target.resize(N + 1);
		for (int i = 0; i < N; i++) {
			cin >> target[i];
		}
		target.push_back(0);

		// calc
		vector<long long> ab;
		ab.reserve((N + 1) * (N + 1));
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= N; j++) {
				ab.push_back(target[i] + target[j]);
			}
		}

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

		long long res = 0;
		for (int i = 0; i < ab.size(); i++) {
			if (ab[i] > M) {
				break;
			}
			vector<long long>::iterator it = upper_bound(ab.begin(), ab.end(), M - ab[i]);
			res = max(res, ab[i] + *(it - 1));
		}
		
		// output
		cout << res << endl;
	}

	return 0;
}

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

pair<int,pair<int,int>> P(int a,int b,int c){
	pair<int,pair<int,int>> ret;
	ret.first=a; ret.second.first=b; ret.second.second=c;
	return ret;
}

int main(){
while(1){
	int n,m,a,ans=0;
	vector<int>p,pp;
	scanf("%d%d",&n,&m);
	if(n==0&&m==0)break;
	p.push_back(0);
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		p.push_back(a);
	}
	sort(p.begin(),p.end());
	priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;
	pq.push(P(0,0,0));
	while(!pq.empty()){
		pair<int,pair<int,int>> pai=pq.top(); pq.pop();
		if(pai.first<=m){
			pp.push_back(pai.first);
			pq.push(P(p[pai.second.first]+p[pai.second.second+1],pai.second.first,pai.second.second+1));
			if(pai.second.second==0)pq.push(P(p[pai.second.first+1]+p[pai.second.second],pai.second.first+1,pai.second.second));
		}
	}
	int s=pp.size()-1;
	for(int i=0;i<=s;i++){
		while(pp[i]+pp[s]>m)s--;
		if(ans<pp[i]+pp[s])ans=pp[i]+pp[s];
	}
	printf("%d\n",ans);
}
}

C++: #include<bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;


signed main() {
	
	while(1) {
		cin2(n,m);
		if (n + m == 0) break;

		vi p(n,0);
		vi v;
		rep(i,0,n) {
			cin1(p[i]);
			v.pb(p[i]);
		}

		rep(i,0,n) {
			rep(j,0,n) {
				if (p[i] + p[j] <= m) { 
					v.pb(p[i] + p[j]);	
				}
			}
		}
		sort(v.begin(),v.end());

		int size = v.size();

		int ans = 0;
		rep(i,0,size) {
			int c_d = v[i];
			int a_b = *(upper_bound(v.begin(),v.end(),m - c_d) - 1);
			ans = max(ans,a_b + c_d);
		}
		cout(ans);
	}



	return 0;
}

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

int N, M;
int P[1000];
int D[1000000];
int S;

int main() {
  while (cin >> N >> M) {
    if (N == 0 && M == 0) break;
    for (int i=0; i<N; i++) cin >> P[i];
    D[0] = 0;
    for (int i=0; i<N; i++) {
      D[i+1] = P[i];
      for (int j=0; j<N; j++) {
        D[(i+1)*N + j + 1] = P[i] + P[j];
      }
    }
    S = N*(N+1) + 1;
    sort(D, D + S);
    int ans = 0;
    for (int i=0; i<N*N; i++) {
      ans = max(ans, D[i] + *(upper_bound(D, D + S, M - D[i]) - 1));
    }
    cout << ans << "\n";
  }
  return 0;
}

C++: #include<stdio.h>
using namespace std;
 
int main() {
	while(1){
		/*入力*/
		int N,M,count=0;
		scanf("%d%d",&N,&M);
		if(N==0 && M==0)return 0;
		int P[N+1],array[M+1]={0};
		P[0]=0;
		for(int i=1;i<=N;i++)scanf("%d",&P[i]);
		for(int i=0;i<=N;i++)array[P[i]]=1;
 
		/*２投(０投１投を含む)で1からMまでが可能か判定*/
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				if(P[i]+P[j] <= M)array[P[i]+P[j]]=1;
			}
		}
 
		/*４投に拡張*/
		for(int i=0;i<=M;i++){
			if(array[i]==1)count++;
		}
		int k=0,p[count];
		for(int i=0;i<=M;i++){
			if(array[i]==1){
				p[k]=i;
				k++;
			}
		}
		for(int i=0;i<count;i++){
			for(int j=0;j<count;j++){
				if(p[i]+p[j] <= M)array[p[i]+p[j]]=1;
			}
		}
 
		/*判定出力*/
		for(int i=M;i>0;i--){
			if(array[i]==1){
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

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

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

		vector<ll> v(n);
		rep(i, n) cin >> v[i];

		vector<ll> p;
		rep(i, n) p.push_back(v[i]);

		rep(i, n) {
			REP(j, i, n) {
				p.push_back(v[i] + v[j]);
			}
		}

		sort(p.begin(), p.end());
		ll ans = 0;
		rep(i, p.size()) {
			int id = upper_bound(p.begin(), p.end(), m - p[i]) - p.begin();
			if(id == 0) {
				if(p[i] <= m) ans = max(ans, p[i]);
			}
			else ans = max(ans, p[i] + p[id-1]);
		}

		cout << ans << endl;
	}

	return 0;
}

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

using namespace std;

int Bsearch(int);

vector<int>num2;

int main(void){
int n,m,t;
vector<int>num;

while(cin >> n >> m,n|m){
num.clear();
num2.clear();

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

num.push_back(0);

for(int i=0;i<num.size();i++){
for(int j=0;j<num.size();j++){
num2.push_back(num[i]+num[j]);
}
}
sort(num2.begin(),num2.end());
vector<int>::iterator it = unique(num2.begin(),num2.end());
num2.erase(it, num2.end() );

int ans=0,c=num2.size(),tmp;
for(int i=0;i<c;i++){
tmp=Bsearch(m-num2[i]);
if(tmp+num2[i]>ans && tmp+num2[i]<=m)ans=tmp+num2[i];
}
cout << ans << endl;
}

return 0;
}

int Bsearch(int x){
int l=0,r=num2.size(),mid;

while(r-l>1){
mid=(l+r)/2;
if(num2[mid]>x)r=mid;
else if(num2[mid]==x)return num2[mid];
else if(num2[mid]<x)l=mid;
}
if(num2[mid]>x)return num2[mid-1];
return num2[mid];
}