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 <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;

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

int n;
llong m;

int main(){
	while(1){
		scanf("%d%lld", &n, &m);
		if(n==0 && m==0) return 0;
		n++;
		vector<llong> p(n);
		p[0]=0;
		FOR(i, 1, n) scanf("%lld", &p[i]);
		VSORT(p);
		int size=0;
		vector<llong> v;
		REP(i, n){
			REP(j, n){
				v.push_back(p[i]+p[j]);
			}
		}
		VSORT(v);
		llong ans=0;
		REP(i, v.size()){
			int rem=m-v[i];
			if(rem < 0) continue;
			int l=0, r=v.size();

			while(l<r){
				int mid=(l+r)/2;
				if(rem>v[mid]){
					l=mid+1;
				}
				else if(rem<v[mid]){
					r=mid;
				}
				else {
					break;
				}
			}

			ans = max(ans, v[i]+v[r-1]);
		}

		printf("%d\n",ans);
	}
}

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

	int tsP[1001 * 1001];
	int main() {
		int N, M;
		int P[1001];
		int ans;
		int cnt;

		P[0] = 0;

		//cin >> N >> M;
		while (cin>>N>>M,N&&M) {
			ans = 0;
			cnt = 0;
			for (int i = 1; i <= N; i++) {
				cin >> P[i];
			}

			for (int i = 0; i <= N; i++) {
				for (int j = i; j <= N; j++) {
					if (P[i] + P[j] <= M) {
						tsP[cnt++] = P[i] + P[j];
					}

				}
			}

			for (int i = 0; i < cnt; i++) {
				for (int j = i; j < cnt; j++) {
					if (tsP[i] + tsP[j] <= M) {
						ans = max(ans, tsP[i] + tsP[j]);
					}
				}
			}
			cout << ans << endl;
		}
		return 0;
	}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long n;
	unsigned long long m;
	while(cin>>n>>m,n!=0||m!=0){
		int k=0;
	long long int s=0;
		vector<long long int> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0&&k!=4){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
				k++;
			}
			else
				n--;
			
		}
		cout<<s<<endl;
	}
	
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
while(1){
int i,j,n,m,o=1,s=0;
static int p[1001]={0},up[1001*1001]={0};
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++){
scanf("%d",&p[i]);
for(j=i;j>0;j--){
up[o]=p[i]+p[j];
o++;
}
}
sort(up,up+o);
for(i=0;i<o;i++)s=max(s,up[i]+*(upper_bound(up,up+o,m-p[i])-1));
printf("%d\n",s);
}
return 0;
}

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

int main(){
  int n, m;
  while(1){
    scanf("%d%d", &n, &m);
    if(n == 0 && m == 0)return 0;
    int res = 0, tmp;
    vector<int> points, points2;
    for(int i = 0;i < n;i++){
      scanf("%d", &tmp);
      points.push_back(tmp);
    }
    vector<int>::iterator it1,it2;
    for(it1 = points.begin();it1 != points.end();it1++){
      points2.push_back(*it1);
      for(it2 = points.begin();it2 != points.end();it2++){
	if(*it1 + *it2 <= m)points2.push_back(*it1 + *it2);
      }
    }
    sort(points2.begin(),points2.end());
    for(it1 = points2.begin();it1 != points2.end();it1++){
      it2 = upper_bound(points2.begin(), points2.end(), m - *it1);
      if(it2 == points2.begin())continue;
      it2--;
      res = max(res, *it1 + *it2);
    }
    printf("%d\n",res);
  }
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <cmath>
#include <limits>
#include <functional>

#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define FORR(i,m,n) for(int i=m;i>=n;i--)
#define pb(x) push_back(x) 
#define SORT(x) sort((x).begin(),(x).end())
#define INF 999999999

using namespace std;

#define int long long

signed main(){
	int n, m;
	cin >> n >> m;
	vector<int> p(n);
	REP(i, n){
		cin >> p[i];
	}
	p.push_back(0);
	sort(p.begin(), p.end());
	n++;
	
	vector<int> v;
	REP(i, n){
		REP(j, n){
			v.push_back(p[i]+p[j]);
		}
	}
	SORT(v);
	
	int l=0, r=v.size()-1, ans=0;
	REP(i, 30){
		int k=v[l]+v[r];
		if(k>m){
			if(r>0) r--;
		}
		else if(k<=m){
			ans=max(ans, k);
			if(l<v.size()-1) l++;
		}
	}
	
	cout << ans << endl;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
  int N,M;
  vector<int>P,P2;
  int ans,sum;
  int low,high,medi;
  while(cin>>N>>M,N|M){
    P.resize(N);
    P2.clear();
    for(int i=0;i<N;i++){
      cin>>P[i];
      P2.push_back(P[i]);
    }
    for(int i=0;i<N;i++)for(int j=i;j<N;j++)P2.push_back(P[i]+P[j]);
    sort(P2.begin(),P2.end());
    ans=0;
    for(int i=0;i<P2.size();i++){
      low=0;
      high=P2.size();
      medi=(low+high)/2;
      while(low<high && low!=medi){
	sum=P2[i]+P2[medi];
	if(sum>M)high=medi;
	else low=medi;
	medi=(low+high)/2;
      }
      sum=P2[i]+P2[medi];
      if(sum>M)
	if(medi>0)medi--;
	else sum=0;
      ans=max(ans,sum);
    }
    cout<<ans<<endl;
  }
}

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

#define MAX_N 1010
#define pb push_back

int main(){
  int N, M;
  
  while(cin >> N >> M){
    if(N == 0 && M == 0) break;
    vector<int> P(N), P2;

    for(int i = 0 ; i < N ; i++){
      cin >> P[i];
      P2.pb(P[i]);
    }

    P2.pb(0);
    for(int i = 0 ; i < N ; i++){
      for(int j = 0 ; j < N ; j++){
	P2.pb(P[i]+P[j]);
      }
    }

    sort(P2.begin(), P2.end());
    
    int ans = 0;
    for(int i = 0 ; i < P2.size() ; i++){
      if(M >= P2[i]){
	vector<int>::iterator it = upper_bound(P2.begin(), P2.end(), M-P2[i]);
	it--;
	ans = max(ans, P2[i]+ *it);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
while(1){
int i,j,n,m,o=1,s=0;
static int p[1001]={0},up[1001*1001]={0};
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++){
scanf("%d",&p[i]);
for(j=i;j>0;j--){
up[o]=p[i]+p[j];
o++;
}
}
sort(up,up+o);
for(i=0;i<o;i++){
if(*(upper_bound(up,up+o,m-up[i])-1)<=m-up[i])s=max(s,up[i]+*(upper_bound(up,up+o,m-up[i])-1));
}
printf("%d\n",s);
}
return 0;
}