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*len(arr)+j] = arr[i] + arr[j]
			}
		}

		sort.Slice(sums, func(i, j int) bool { return sums[i] < sums[j] })
		var m int64 = 0
		for _, val := range sums {
			rem := M - val
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, val+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
/*bool ok(int *q,int m,int h,int count){
  for(int i=0;i<count;i++){
    if(q[i]>m) continue;
    for(int j=0;j<count;j++){
      if(q[i]+q[j]<=m&&q[i]+q[j]>h) return true;
    }
  }
  return false;
  }*/
int main(){
  int n,m;
  while(cin>>n>>m){
    if(n==0&&m==0) return 0;
    int data[n+1];
    int q[(n+1)*(n+1)/2];
    int ans = 0;
    for(int i=0;i<n;i++) cin >> data[i];
    data[n] = 0;
    int count = 0;
    for(int i=0;i<=n;i++){
      for(int j=i;j<=n;j++){
	if(data[i]+data[j]<=m) q[count] = data[i] + data[j];
	count++;
      }
    }
    //sort(q,q+count);
    for(int i=0;i<count;i++){
      for(int j=0;j<count;j++) if(q[i]+q[j]>ans&&q[i]+q[j]<=m) ans = q[i] + q[j];}
    cout << ans << endl;
  }
}

C++: #include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main(){
  
  int n, m, p[1111];
  int sum[111111];

  while(cin >> n >> m, n||m){
    for(int i = 0; i < n; i++) cin >> p[i];
    
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        sum[i * n + j] = p[i] + p[j];
      }
    }
    
    int maxi = 0;
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        if(sum[i * n + j] <= m){
          maxi = max(maxi, sum[i * n + j]);
          if(sum[i * n + j] + p[i] + p[j] <= m)
            maxi = max(maxi, sum[i * n + j] + p[i] + p[j]);
          if(sum[i * n + j] + p[i] <= m)
            maxi = max(maxi, sum[i * n + j] + p[i]);
          if(sum[i * n + j] + p[j] <= m)
            maxi = max(maxi, sum[i * n + j] + p[j]);
        }
      }
    }
    cout << maxi << endl;
  }
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<algorithm>
 
using namespace std;
 
int main(){
  while(true){
    int N,M,mex;
    mex = -1;
    cin >> N >> M ;
    if(N+M == 0)break;
    vector<int> val,dp;
    for(int i=0;i<N;i++){
      int a; cin >> a; 
      val.push_back(a);     
      dp.push_back(a);
    }
    dp.push_back(0);
    map<int,bool> exist;
    for(int i=0;i<val.size();i++){
      for(int j=0;j<val.size();j++){
    //if(M > val[i]+val[j])continue;
    dp.push_back(val[i]+val[j]);
      }
    }
    sort(dp.begin(),dp.end());
    int mmex = -1; 
    for(int i=0;i<dp.size();i++){
      if(M >= dp[i])mmex = max(mmex,dp[i]+upper_bound(dp.begin(),dp.end(),M-dp[i])[-1]);
       
    }
 
    cout << mmex << endl;
    mex = -1;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;


signed main() {
	
	while(1) {
		cin2(n,m);
		if (n + m == 0) break;

		vi p(n,0);
		vi v;
		rep(i,0,n) {
			cin1(p[i]);
			v.pb(p[i]);
		}

		rep(i,0,n) {
			rep(j,0,n) {
				if (p[i] + p[j] <= m) { 
					v.pb(p[i] + p[j]);	
				}
			}
		}
		sort(v.begin(),v.end());

		int size = v.size();

		int ans = 0;
		rep(i,0,size) {
			int c_d = v[i];
			int a_b = *(upper_bound(v.begin(),v.end(),m - c_d) - 1);
			ans = max(ans,a_b + c_d);
		}
		cout(ans);
	}



	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 P[1002];
INT Q[1002001];

INT solve( int N, INT M ) {
    INT answer = 0;
    for ( int i = 0; i < N; ++ i ) {
        int q = Q[i];
        int L = i;
        int R = N;
        INT m = M - q;
        while ( L < R ) {
            int p = ( R - L ) / 2 + L;
            INT t = q + Q[p];
            if ( t <= M ) 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;
        for ( int i = 0; i < n; ++ i ) {
            INT p;
            cin >> p;
            P[i] = p;
        }
        P[n] = 0;
        for ( int i = 0; i < n + 1; ++ i ) {
            for ( int j = 0; j < n + 1; ++ j ) {
                Q[i * ( n + 1 ) + j] = P[i] + P[j];
            }
        }
        int length = ( n + 1 ) * ( n + 1 );
        sort( Q, Q + length );
        cout << solve( length, m ) << endl;
    }
    return 0;
}