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<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];
}

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;
	}
}