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

int N,M;
void solve()
{
	int res=0;
	vector<int>first,second;
	int temp;
	first.push_back(0);
	for(int i=0;i<N;i++)
	{
		scanf("%d",&temp);
		first.push_back(temp);
	}
	sort(first.begin(),first.end());
	for(int i=0;i<first.size();i++)
	{
		for(int j=i;j<first.size();j++)
		{
			if(first[i]+first[j]<=M)
			{
				second.push_back(first[i]+first[j]);
			}else{
				break;
			}
		}
	}
	sort(second.begin(),second.end());
	for(int i=0;i<second.size();i++)
	{
		int findV=M-second[i];
		int left=0,right=second.size()-1;
		int mid;
		int max_min=0;
		while(left<=right)
		{
			mid=(left+right)/2;
			if(findV<second[mid]){
				right=mid-1;
			}else{
				max_min=max(max_min,second[mid]);
				left=mid+1;
			}
		}
		res=max(res,max_min+second[i]);
	}
	printf("%d\n",res);
}

int main()
{
	while(true)
	{
		scanf("%d%d",&N,&M);
		if(N==0&&M==0)break;
		solve();
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; ++i)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define SCAN(x) cin >> x
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define PB push_back
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<long double> P;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> Point;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

#ifdef WIN32
#define dump(x) cerr << #x << " = " << (x) << "\n"
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif

template<class T> ostream &operator<<( ostream &os, vector<T> &v ) {
  for ( int i = 0; i < v.size(); i++ ) {
    if ( i ) os << " ";
    os << v[ i ];
  }
  return os;
}

int main() {
  ios::sync_with_stdio( false );
  cin.tie( 0 );
  int n, m;
  while ( cin >> n >> m && n && m ) {
    VI p( n );
    for ( int i = 0; i < n; i++ ) {
      cin >> p[ i ];
    }
    p.push_back( 0 );
    SORT( p );
    set<int> two_selected;
    for ( int i = 0; i < n + 1; i++ ) {
      for ( int j = i; j < n + 1; j++ ) {
        two_selected.insert( p[ i ] + p[ j ] );
      }
    }
    int ans = 0;
    for ( set<int>::iterator it = two_selected.begin(); it != two_selected.end(); ++it ) {
      set<int>::iterator it2 = two_selected.upper_bound( m - *it );
      if ( it2 != two_selected.begin() ) {
        --it2;
        ans = max( ans, *it + *it2 );
      }
    }
    PRINT( ans );
  }
  return 0;
}

C++: 
#include<bits/stdc++.h>

#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repm(a,b,c) for(int a=(b-1);a>=c;a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d",&a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s){double ans=0;rep(i,0,s.length()){ans+=(s[i]-'0')*pow(10,s.length()-i-1);}return (int)ans;}
inline string numstr(int m){str s="";while(m>0){char c;int a=m/10;if(a>0)a=m%(a*10);else a=m;c=(char)('0'+a);s+=c;m/=10;}str st="";for(int i=s.size()-1;i>=0;i--){st+=s[i];}return st;}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
vi cost;
int d[1010];
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,k;
    while(1){
    cost.clear();
    cin >> n >> k;
    if(n==0&&k==0)break;
    rep(i,0,n){
        cin >> d[i];
    }
    d[n]=0;
    int cou=0;
    rep(i,0,n+1){
        rep(j,0,n+1){
            int u=d[i]+d[j];
            cost.pb(u);
        }
    }
    sort(ALL(cost));
    int ans=0;
    for(int v : cost){
           // cout << v << endl;
        int an=k-v;
        if(an<0)break;
        int smal=0,larg=cost.size()-1;
        while(larg>smal){
            int cen=(smal+larg)/2;
            //cout << cen << endl;
            if(cost[cen]>an){
                larg=cen;
            }else if(cost[cen]<an){
                smal=cen+1;
            }else{
                ans=k;
                break;
            }
            if(larg<=smal){
                int a=cost[larg-1];
                if(ans<(v+a))ans=v+a;
                break;
            }
        }
        if(ans<(v+cost[larg-1]))ans=(v+cost[larg]);
    }
    cout << ans << endl;
    }
    return 0;
}

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

int n,m;
int target[1000];

int chek(int cnt, int point)
{
	int i;
	int sum=0;

	if (cnt == 5)
	{
		return point;
	}

	for (i = 0; i <= n; i++)
	{
		if (point + target[i] <= m)
		{
			sum = max(sum, chek(cnt + 1, point + target[i]));

			/*
			num = chek(cnt + 1, point + target[i]);
			if (num <= m)
			{
				max = (max < num) ? max : num;
			}
			*/
		}
	}

	return sum;
}

int main()
{
	int sum;
	int i;

	while (scanf("%d %d",&n,&m), n != 0 || m != 0)
	{
		//printf("AA%d\n",m);
		for (i = 1; i <= n; i++)
		{
			scanf("%d", &target[i]);
		}

		sum = chek(1, 0);

		printf("%d\n", sum);
	}

	return 0;
}

C++: #include <iostream>
#include <map>
#include <algorithm>
#define INF (2LL << 58)
using namespace std;

typedef long long ll;

map<int, ll> dp;
int n, m, p[1111];

ll rec(int sum, int cnt){
    
    if(sum < 0) return -INF;
    if(cnt == 4) return 0;
    if(dp[sum]) return dp[sum];
    
    ll ret = -INF;
    for(int i = 0; i < n; i++){
        ret = max(ret, max(rec(sum - p[i], cnt + 1) + p[i], rec(sum, cnt + 1)));
    }
    return dp[sum] = ret;
}

int main(){
    
    while(cin >> n >> m, n||m){
        dp.clear();
        for(int i = 0; i < n; i++) cin >> p[i];
        sort(p, p + n, greater<int>());
        cout << rec(m, 0) << endl;
    }
}