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 <string.h>
#include <algorithm>
#include <set>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
	int N, M;
	while(cin >> N >> M && (N || M)){
		int  P[1001] = {0}, ans = 0;
		for(int i = 0; i < N; ++i){
			scanf("%d", &P[i]);
		}
		vector<int> v;
		for(int i = 0; i <= N; ++i){
			for(int j = 0; j <= N; ++j){
				if(P[i] + P[j] <= M)
					v.push_back(P[i] + P[j]);
			}
		}
		sort(v.begin(), v.end());
		v.erase(unique(v.begin(), v.end()), v.end());
		for(unsigned int i = 0; i < v.size(); ++i){
			if(v[i] > M)
				continue;
			vector<int>::iterator it = lower_bound(v.begin(), v.end(), M - v[i]);
			if(it == v.end())
				--it;
			if(*it > M - v[i])
				--it;
					
			ans = max(ans, v[i] + *it);
		}
		/*for(int i = 0; i <= N; ++i){
			for(int j = 0; j <= N; ++j){
				if(P[i] + P[j] > M)
					continue;
				set<int>::iterator it = darts.lower_bound(M - (P[i] + P[j]));
				if(it == darts.end())
					--it;
				if(*it > M - (P[i] + P[j]))
					--it;
					
				ans = max(ans, P[i] + P[j] + *it);
			}
		}*/
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1002002];
int main(void){
	while(1){
		ll n,m,i,j,p[1001],hi,lo,mi,pp,x,mx;
		cin>>n>>m;
		if(n==0)break;
		p[0]=0;
		for(i=1;i<=n;i++) cin>>p[i];
		for(i=0;i<=n;i++) for(j=0;j<=n;j++) a[i*n+j]=p[i]+p[j];
		pp=pow(n+1,2);
		sort(a,a+(pp+2));
//		for(i=0;i<=pp+1;i++) cout<<a[i]<<endl;
		mx=0;
		for(i=0;i<=pp+1;i++){
			hi=pp+1; lo=0;
			while(hi>=lo){
				mi=(hi+lo)/2;
				x=a[i]+a[mi];
				if(x>m) hi=mi-1;
				else if(x<m) lo=mi+1;
			}
			x=a[i]+a[hi];
			if(mx<x&&m>=x) mx=x;
		}
		cout<<mx<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)


typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vvi;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007



const int MAX_N = 1000;
const int MAX_W = 200000000;

int dp[MAX_N + 1][MAX_W + 1][5];
int v[MAX_N], w[MAX_N];
int N,W;


int solve() {
	for(int i = 0; i < N; i++) {
		for(int j = 0; j <= W; j++){
			for(int k = 0; k < 5; k++){
				if(j < w[i] || k <1) {
					dp[i + 1][j][k] = dp[i][j][k];
				}
				else{
					dp[i + 1][j][k] = max(max(dp[i][j][k],dp[i][j-w[i]][k-1] + v[i]), dp[i + 1][j - w[i]][k-1] + v[i]);
				}
			}
		}
	}
	return dp[N][W][4];
}

int main(){
	int n, m;
	while(cin>>n>>m){
		N = n;
		W = m;
		for(int i = 0; i < n; i++){
			cin >> v[i];
			w[i] = v[i];
		}
		cout<<solve()<<endl;
	}
}

C++: #include <iostream>
using namespace std;
 
int main() {
	for(;;){
		/*入力*/
		int N,M,count=0;
		cin >> N >> M;
		if(N==0 && M==0)exit(0);
		int P[N+1],array[M+1]={0};
		P[0]=0;
		for(int i=1;i<=N;i++)cin >> 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){
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
	while(1){
	int n,m,p,q=0,r=0,l=0,s=0,e=0,ans=0;
	cin>>n>>m;
		if(n==0 && m==0)
			break;
	vector<int> a,x;
	a.push_back(0);
	for(int i=0;i<n;i++){
		cin>>p;
		a.push_back(p);
	}
	q=n+1;
	//cout<<q<<endl;
	for(int i=0;i<q;i++){
		for(int j=0;j<q;j++){
			x.push_back(a[i]+a[j]);
		}
	}
	sort(x.begin(),x.end());
	//for(int i=0;i<q*q;i++)
	//cout<<x[i]<<' ';
	//cout<<endl;
	for(int i=0;i<x.size();i++){
		r=m-x[i];
		//cout<<r<<"を探します"<<endl;
		if(r>=0){
			s=0;
			e=q*q;
			l=(s+e)/2;
			while(s<e){
				//cout<<"l="<<l<<endl;
				l=(s+e)/2;
				if(x[l]==r)break;
				if(x[l]>r){
					e=l-1;
				}else{
					s=l+1;
				}
			}
			while(x[l]>r&&p>0)
			l--;
			while(1){
				if(l+1==q*q)break;
				if(x[l+1]<=r)l++;
				else break;
			}
		ans=max(ans,x[i]+x[l]);
		}
	}
		cout<<ans<<endl;
	}
	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();
		S.insert(0);
		for ( int i = 0; i < n; ++i )
		{
			cin >> p[i];
			for ( int j = 1; j <= 4; ++j )
			{
				if ( (p[i] * j) <= m )
				{
					S.insert(p[i]);
					ans = max( ans, (p[i] * j) );
				}
			}
		}
		
		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, x );
					int y = m - x;
					while ( !S.count(y) && ( ans < (x + y) ) )
					{
						--y;
					}
					ans = max( ans, (x + y) );
				}
			}
		}
		
		if ( S.count(m) )
		{
			cout << "m=" << m << endl;
			continue;
		}
		
		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 left = 0;
        int right = n;
        int m = M - Q[i];
        while ( left < right ) {
            int p = ( right - left) / 2 + left;
            if ( Q[p] <= m ) {
                answer = max( answer, Q[i] + Q[p] );
                left = p + 1;
            } else {
                right = p;
            }
        }
    }
    return answer;
}

int main() {
    int n, m;
    while ( cin >> n >> m ) {
        if ( n == 0 && m == 0 ) break;
        VI P( n );
        for ( int i = 0; i < n; ++ i ) {
            cin >> P[i];
        }
        SI Q_;
        Q_.insert( 0 );
        for ( int i = 0; i < n; ++ i ) {
            Q_.insert( P[i] > m ? 0 : P[i] );
            for ( int j = 0; j < n; ++ j ) {
                Q_.insert( ( P[i] + P[j] > m ? 0 : P[i] + P[j] ) );
            }
        }
        VI Q( Q_.begin(), Q_.end() );
        cout << solve( Q, m ) << endl;
    }
    return 0;
}