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<bits/stdc++.h>
using namespace std;

typedef long long ll;

int main(void){
    ll n, m, p[2100];
    while(cin>>n>>m){
        if(n+m==0) break;
        p[0]=0;
        ll maxp=0;
        for(int i=1; i<=n; i++){
            cin >> p[i];
            if(p[i]<=m) maxp=max(maxp, p[i]);
        }
        int now=n+1;
        for(int i=0; i<=n; i++){
            for(int j=i; j<=n; j++){
                p[now]=p[i]+p[j];
                if(p[now]<=m) maxp=max(maxp, p[now]);
                now++;
            }
        }
        sort(p, p+now);
        ll maxa=m-maxp;
        ll under=0, upper=now;
        while(upper-under>1LL){
            ll mid=(under+upper)/2;
            if(maxa>=p[mid]){
                under=mid;
            }
            else{
                upper=mid;
            }
        }
        cout << (maxp+p[under]) << "\n";
        
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n >> m && n && m){
    vary(ll,s,n,0);
    rep(i,n){
      cin >> s[i];
    }
    sort(all(s));
    vector<vector<ll>> dp(6,vector<ll>(n+1,0));
    rep(i,4){
      rep(k,n){
        rep(j,n){
          if(dp[i][j] + s[k] <= m){
            dp[i+1][k] = max(dp[i+1][k],dp[i][j]+s[k]);
          }
        }
      }
    }
//     rep(i,5){
//       rep(j,n){
//         cout << dp[i][j] << " ";
//       }
//       cout << endl;
//     }
    ll ans = 0;
    rep(i,5){
      rep(j,n){
        ans = max(ans,dp[i][j]);
      }
    }
    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 - 1;
            } else {
                L = p + 1;
            }
        }
    }
    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;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define int             long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )    for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )  FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP              make_pair

typedef pair<int, int> P;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int INFLL = 1e18;
const int MOD = 1e9 + 7;

signed main(){
    int N, M;
    while( cin >> N >> M, N | M ){
        vector<int> p( N + 1 );
        REP( i, N ) cin >> p[i];
        p[N] = 0;

        vector<int> sum( ( N + 1 ) * ( N + 1 ) );
        int size = 0;
        REP( i, N + 1 ){
            REP( j, N + 1 ){
                sum[size++] = p[i] + p[j];
            }
        }

        sort( ALL( sum ) );

        int ans = 0;
        REP( i, ( N + 1 ) * ( N + 1 ) ){
            auto it = lower_bound( ALL( sum ), M - sum[i] );
            if( it != sum.begin() ){
                chmax( ans, sum[i] + *prev( it ) );
            }
        }

        cout << ans << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,p[1001];
vector<int>v;
int nibuntansaku(int x){
  int l=0,r=v.size();
  while(l<r){
    int mid=(l+r)/2;
    if(v[mid]>x)r=mid;
    else l=mid+1;
  }
  if(v[l]>x&&l!=0)l--;
  return v[l];
}
main(){
  while(cin>>n>>m,n){
    v.clear();
    v.push_back(0);
    for(int i=0;i<n;i++){
      cin>>p[i];
      v.push_back(p[i]);
    }
    for(int i=0;i<n;i++)
      for(int j=i+1;j<n;j++)
        v.push_back(p[i]+p[j]);
    sort(v.begin(),v.end());
    int sum=0;
    for(int i=0;i<v.size();i++){
      int x=nibuntansaku(m-v[i])+v[i];
      if(x<=m)sum=max(x,sum);
    }
    cout<<sum<<endl;
  }
}