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+j] = arr[i] + arr[j]
			}
		}
		sort.Slice(sums, func(i, j int) bool { return sums[i] < sums[j] })

		var m int64 = 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<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;
}

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

using namespace std;

int main ( void )
{
	int n, m;
	int p[1000];
	set<int> S;
	
	while ( cin >> n >> m, (n | m) )
	{
		int ans = 0;
		S.clear();
		for ( int i = 0; i < n; ++i )
        {
            cin >> p[i];
            if ( p[i] < m )
            {
                S.insert(p[i]);
                ans = max( ans, p[i] );
            }
        }
        for ( int i = 0; i < n; ++i )
        {
            for ( int j = i; j < n; ++j )
            {
                int x = p[i] + p[j];
                if ( x <= m )
                {
                    S.insert(x);
                    ans = max( ans, p[i] );
                }
            }
        }
        
        for ( int i = 0; i < n; ++i )
        {
            for ( int j = i; j < n; ++j )
            {
                int x = p[i] + p[j];
                if ( x <= m )
                {
                	int y = m - x;
                    set<int>::iterator it = lower_bound( S.begin(), S.end(), y );
                    if ( *it > y )	--it;
                    ans = max( ans, (x + *it) );
                }
            }
        }
		cout << ans << endl;
	}
	
	return 0;
}

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

using namespace std;

int main() {
  
  int n, m;
  
  while(cin >> n >> m && (n|m)) {
    int data[1002];
    int Q[10000005];
    
    n++;
    data[0] = 0;
    for(int i=1; i<n; i++) {
      cin >> data[i];
    }
    
    int T = 0;
    
    for(int i=0; i<n; i++) {
      Q[T++] = data[i];
    }
    
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
	Q[T++] = data[i] + data[j];
      }
    }
    
    sort(Q, Q+T);

    // for(int i=0; i<T; i++) cout << Q[i] << endl;
    
    int ans = 0;
    
    for(int i=0; i<T; i++) {
      int M = m - Q[i];
      if(M < 0) break;
      
      int lb = 0, ub = T;
      
      while(ub - lb > 1) {
	int mid = (lb + ub) / 2;
	
	if(Q[mid] <= M) {
	  lb = mid;
	} else {
	  ub = mid;
	}
      }
      ans = max(ans, Q[i]+Q[lb]);
    }
    
    cout << ans << endl;
  }
  
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,m,a[2000];
vector<int> t;
int main(){
  a[0]=0;
  while(cin>>n>>m&&n&&m){
    t.clear();
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=0;i<n;i++){
      for(int j=i;j<=n;j++){
	t.push_back(-(a[i]+a[j]));
      }
    }
    sort(t.begin(),t.end());
    int ans=0;
    for(int i=0;i<(int)t.size();i++){
      int x=-t[i];
      int y=m-x;
      if(y<0)continue;
      vector<int> :: iterator it;
      it = lower_bound(t.begin(),t.end(),-y);
      if(it==t.end())continue;
      int z = *it;
      ans=max(ans,x-z);
    }
    cout<<ans<<endl;
  }
  return 0;
}