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 <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;
	while(1){
		cin >> n >> m;
		if(n==0) break;
		
		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){
				if(p[i]+p[j]<=m) v.push_back(p[i]+p[j]);
			}
		}
		sort(v.begin(), v.end());
		
		int l=0, r=v.size()-1, ans=0;
		for(; l<=r; l++){
			if(v[l]+v[r]>m) r--;
			ans=max(ans, v[l]+v[r]);
		}
		
		cout << ans << endl;
	}
}

C++: #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>

using namespace std;


void func(int N,int M){
    vector<int> First,Second;

    First.push_back(0);
    int tmp,max = 0;

    for(int i = 0; i < N; i++){
        scanf("%d",&tmp);
        if(tmp <= M){
        	First.push_back(tmp);
        }
    }

    Second.push_back(0);
    for(int i = 0; i < First.size();i++){
    	for(int k = i; k < First.size();k++){
    		if(First[i]+First[k] <= M){
    			Second.push_back(First[i]+First[k]);
    		}
    	}
    }

    for(int i = 0; i < Second.size();i++){
    	for(int k = i; k < Second.size();k++){
    		if(Second[i]+Second[k] <= M && Second[i]+Second[k] > max){
    			max = Second[i]+Second[k];
    		}
    	}
    }

    printf("%d\n",max);

}

int main(){

    int N,M;

    while(true){
        scanf("%d %d",&N,&M);
        if(N == 0 && M == 0)break;

        func(N,M);
    }

    return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
//http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
#include<cstring>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define P pair<int,int>
#define ll long long 

ll n,m;
ll t[10000];
const int MA=1000000;
ll s1[MA];
ll s2[MA];


int main(){

while(cin>>n>>m,n||m){
rep(i,MA)s1[i]=s2[i]=0;
rep(i,10000)t[i]=0;

for(int i=1;i<=n;i++)cin>>t[i];
int g=0;

for(int i=0;i<=n;i++){
	for(int j=0;j<=n;j++){
		s1[i*(n+1)+j]=t[i]+t[j];
		g++;
	}
}


sort(s1,s1+g);
rep(i,g)s2[i]=s1[i];

ll ans=0;

for(int i=0;i<g;i++){
	int now=s1[i];

	int l=0;
	int r=g-1;
	int mid=(l+r)/2;
	ll x=s2[mid];

	for(int i=0;i<70;i++){
		if(x+now==m)break;
		if(l>r)break;
		if(x+now>m){
			r=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
		else{
			l=mid;
			mid=(r+l)/2;
			x=s2[mid];
		}
	}

	//cout<<" "<<x<<" "<<now<<" "<<x+now<<endl;
	if(x+now<=m)
	ans=max(ans,x+now);
}
cout<<ans<<endl;
}
	return 0;
}

C++: #include "stdio.h"
#include <iostream>
#include <algorithm>

using namespace std;

long long int gten[1000000];
long long int p[1111];

long long int ans[1000];

int main()
{
  int ww = 0;
  while(1)
    {
      long long int n,m;
      scanf("%lld %lld",&n,&m);
      if(n == 0 && m == 0)
	{
	  break;
	}
      p[0] = 0;
      for(int i = 1; i <= n; i++)
	{
	  scanf("%lld",&(p[i]));
	}
      int nw = 0;
      for(int i = 0; i <= n; i++)
	{
	  for(int ii = 0; ii <= n; ii++)
	    {
	      gten[nw] = p[i] + p[ii];
	      nw++;
	    }
	}
      sort(gten,gten + nw);
      long long int max = 0;
      int ai = 0;
      for(int i = nw - 1; i >= 0; i--)
	{
	  while(1)
	    {
	      if(ai + 1 >= nw)
		{
		  break;
		}
	      if(gten[i] + gten[ai + 1] <= m)
		{
		  ai++;
		  if(max < gten[i] + gten[ai])
		    {
		      max = gten[i] + gten[ai];
		    }
		}
	      else
		{
		  break;
		}
	    }
	}
      ans[ww] = max;
      ww++;
    }
  for(int i = 0; i < ww; i++)
    {
      printf("%lld\n",ans[i]);
    }
  return 0;
}

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

int kazu[1000000];
int n,m,Max,data[1000];
int main(){
	while(true){
		int s=0;
		std::cin>>n>>m;
		if(n==0&&m==0)break;
		data[0]=0;
		for(int i=0;i<n;i++){
			std::cin>>data[i+1];
		}
		for(int c=0;c<n+1;c++){
			for(int d=0;d<n+1;d++){
				kazu[c*n+d]=data[c]+data[d];
			}
		}
		std::sort(kazu,kazu+n*n);
		for(int a=0;a<n+1;a++){
			for(int b=0;b<n+1;b++){
				while(s<m){
					if(std::binary_search(kazu,kazu+n*n,m-s-data[a]-data[b])){
						Max=std::max(m-s,Max);
						break;
					}
					s++;
				}
				s=0;
			}
		}
		std::cout<<Max<<std::endl;
		Max=0;
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>
#include<algorithm>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)

int main(){
	while(1){
		int n,T;
		cin>>n>>T;
		if(n==0)break;
		
		int ya[1111];
		ya[0]=0;
		reps(i,n){
			cin>>ya[i];
		}
		n++;
		
		vector<int> pt;
		rep(i,n){
			rep(j,n){
				pt.push_back(ya[i]+ya[j]);
			}
		}
		
		sort(pt.begin(),pt.end());
		
		int maxi=0;
		
		rep(i,pt.size()){
			if(pt[i]>T)break;
			vector<int>::iterator a;
			a=lower_bound(pt.begin(),pt.end(),T-pt[i]);
			
			maxi = max(maxi, pt[i] + *(a-1));
		}
		
		printf("%d\n",maxi);
	}
}

C++: 4 50
3
14
15
9
3 21
16
11
2
0 0

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

int main() {
  while(true) {
    int N, M; cin >> N >> M;
    if(N == 0 && M == 0) break;
    vector<int> A(N);
    for(int i = 0; i < N; i++) {
      cin >> A[i];
    }

    A.push_back(0);
    vector<int> B(N*N);
    for(int a : A) {
      for(int b : A) {
        B.push_back(a+b);
      }
    }
    sort(B.begin(), B.end());
    B.erase(unique(B.begin(), B.end()), B.end());

    int ans = 0;
    for(int i = 0; i < (int)B.size(); i++) {
      if(B[i] > M) continue;
      if(B[i] == M) { ans = M; continue; }
      ans = max(ans, B[i] + *(lower_bound(B.begin(), B.end(), M - B[i]) - 1));
    }
    cout << ans << endl;
  }
}