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 <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

typedef long long ll;

using namespace std;

int mato[1001],n,m,ans;

void nibun(){
    
}

void solve(){
    vector<int> v;
    ans=0;
    
    for(int i=0;i<n;i++)
        scanf("%d",&mato[i]);
    
    mato[n]=0;
    
    sort(mato,mato+n+1);
    
    for(int i=0;i<=n;i++)
        for(int j=i;j<=n;j++)
            if(mato[i]+mato[j]<=m)
                v.push_back(mato[i]+mato[j]);
    
    sort(v.begin(),v.end());
    
    v.push_back(200000010);
    
    for(int i=0;i<v.size();i++){
        
        int l=0,r=(int)v.size()-1,s=m-v[i];
        
        if(s<=0) continue;
        
        while(1){
            if(l==r-1 || v[l]==v[r])
                break;
            
            if(v[(l+r)/2]>s)
                r=(l+r)/2;
            else
                l=(l+r)/2;
        }
        
        ans=max(ans,v[l]+v[i]);
    }
    
    printf("%d\n",ans);
    
    return;
}

int main(){
    
    while(1){
        scanf("%d%d",&n,&m);
        if(n+m==0)
            break;
        solve();
    }
    
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> a;
vector <int> all;
int n,m;



int bainary_search(int i) {
  int L=0;int R = all.size()-1,M;
  
  while(L<R) {
    M = (L+R)/2;
    if(all[i]+all[M] > m) R = M;
    else L = M+1;
  }
   L--;
  return L;
}

int main() {

  while(1) {
    cin >> n >> m;
    if(n == 0 && m ==0) break;
    a.resize(n);
    for(int i=0;i<n;i++) cin >>a[i];

    a.push_back(0);

    for(int i=0;i<a.size();i++) 
      for(int j=i;j<a.size();j++)
	all.push_back(a[i]+a[j]);

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

    int ans = 0;
    for(int i=0;i<all.size();i++) {
      // cout << all[i] <<endl;
      int res = all[i] + all[bainary_search(i)];
      // cout << res <<endl;
      if(ans < res && res<= m) ans = res;
    }
    cout << ans <<endl;
  }
  return 0;
  
}

C++: #include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef long long INT;
typedef vector <INT> VI;
typedef set <INT> SI;

INT solve( VI Q, INT M ) {
    INT answer = 0;
    int n = Q.size();
    for ( int i = 0; i < n; ++ i ) {
        int q = Q[i];
        int L = 0;
        int R = n;
        INT m = M - q;
        while ( L < R ) {
            int p = ( R - L ) / 2 + L;
            INT t = q + Q[p];
            if ( t > M ) t = 0;
            answer = max( answer, t );
            if ( Q[p] > m ) {
                R = p;
            } else {
                L = p + 1;
            }
        }
    }
    return answer;
}

int main() {
    int n, m;
    while ( cin >> n >> m ) {
        if ( n == 0 && m == 0 ) break;
        VI P;
        for ( int i = 0; i < n; ++ i ) {
            INT p;
            cin >> p;
            P.push_back( p );
        }
        P.push_back( 0 );
        VI Q;
        for ( int i = 0; i < n + 1; ++ i ) {
            for ( int j = 0; j < n + 1; ++ j ) {
                Q.push_back( P[i] + P[j] );
            }
        }
        sort( Q.begin(), Q.end() );
        cout << solve( Q, m ) << endl;
    }
    return 0;
}

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		long long int b;
		set<long long int> ss;
		ss.insert(0);
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				ss.insert(b);
			}
		}
		set<long long int> ss2;
		for (set<long long int>::iterator ite = ss.begin(); ite != ss.end(); ite++) {
			ss2.insert(*(ite));
			for (set<long long int>::iterator ite2 = ite; ite2 != ss.end(); ite2++) {
				if (*(ite)+*(ite2) <= m) {
					ss2.insert(*(ite)+*(ite2));
				} else {
					break;
				}
			}
		}
		ss.clear();
		for (set<long long int>::iterator ite = ss2.begin(); ite != ss2.end(); ite++) {
			ss.insert(*(ite));
			for (set<long long int>::iterator ite2 = ite; ite2 != ss2.end(); ite2++) {
				if (*(ite)+*(ite2) <= m) {
					ss.insert(*(ite)+*(ite2));
				} else {
					break;
				}
			}
		}
		set<long long int>::iterator ite3 = ss.end();
		ite3--;
		cout << *(ite3) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<set>
#include<vector>
using namespace std;
typedef unsigned int uint;
uint maxv;
void solve(uint now,uint m,uint *a,int n){
  int r=n-1,l=0,mid;
  while(l <=r){
    mid = (l+r)/2;
    if ( a[mid]+now>m)r=mid-1;
    else if ( a[mid]+now <m)l=mid+1;
    else break;

  }
  if ( now+a[mid]>maxv&& now+a[mid]<=m)maxv=now+a[mid];
}


main(){
  int n;
  uint m;
  while(scanf("%d%u",&n,&m) && n){
    set<uint> S;    
    uint one[n];
    uint two[n*n];
    int p=0;
    for(int i=0;i<n;i++)scanf("%d",&one[i]);
    for(int i=0;i<n;i++){
      for(int j=i;j<n;j++){
	//if ( S.find(one[i]+one[j]) == S.end())S.insert(one[i]+one[j]),two[p++]=one[i]+one[j];
	two[p++]=one[i]+one[j];
      }
    }
    sort(one,one+n);
    sort(two,two+p);
    maxv=0;
    for(int i=0;i<p;i++){
      //if ( two[i]+one[0]>m)break;
      //if ( two[i]+two[0]<=m)solve(two[i],m,two,p);
      //if ( two[i]+one[0]<=m)solve(two[i],m,one,n);
      solve(two[i],m,two,p);
      solve(two[i],m,one,n);
    }
    printf("%u\n",maxv);
  }
  return false;
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int n,m,ans,MAX;
vi d(1010);

void solve(int sc,int k){
	if(sc>=m) return;
	if(sc>MAX) MAX=sc;
	if(k==n) return;
	rep(i,0,n+1){
		solve(sc+d[i],k+1);
	}
}

int main(){
	while(1){
		ans=0,MAX=0;
		cin>>n>>m;
		if(n==0 && m==0) break;
		d[0]=0;
		rep(i,1,n+1) cin>>d[i];
		solve(0,0);
		o(MAX);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(true){
		cin>>n>>m;
		if(n==0)	return 0;
		vector<int> p(n);
		for(int i=0;i<n;i++)	cin>>p[i];
		p.push_back(0);	n++;
		set<int> s;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++)	if(p[i]+p[j]<m)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(set<int>::iterator it=s.begin();it!=s.end();it++){
			set<int>::iterator it2=s.lower_bound(m-*it);
			if(it2!=s.begin())	it2--;
			if(*it+*it2<m)	ans=max(ans,*it+*it2);
			else 	break;
		}
		cout<<ans<<endl;
	}
}