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 <bits/stdc++.h>

using namespace std;

int main()
{
    int N, M;
    while (cin >> N >> M, N) {
        vector<int> a(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                a.push_back(a[i] + a[j]);
            }
        }

        sort(a.begin(), a.end());
        
        N = a.size();
        
        int res = 0;
        for (int i = 0; i < N; i++) {
            int m = M - a[i];
            if (m < 0) break;
            
            int l = 0, r = N;
            while (r - l > 1) {
                int mid = (l + r) / 2;
                if (a[mid] < m) {
                    l = mid;
                    res = max(res, a[i] + a[l]);       
                } else {
                    r = mid;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

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

using namespace std;

int main(void)
{
	int n, m;

	while (cin >> n >> m, n || m){
		vector <int> p(n + 1);
		for (int i = 0; i < n; i++){
			scanf("%d", &p[i]);
		}
		p[n] = 0;
		n++;
		sort(p.begin(), p.end());

		vector <int> sum(n*n);
		int cnt = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				sum[cnt] = p[i] + p[j];
				// cout << sum[cnt] << " " << p[i] << " " << p[j] << endl;
				cnt++;
			}
		}

		int ret = 0;
		for (int i = 0; i < n*n; i++){
			int diff = m - sum[i];
			if (diff < 0) continue;
			int up = *(upper_bound(sum.begin(), sum.end(), diff)-1);
			ret = max(ret, sum[i] + up);
		}

		cout << ret << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,m,k;
  while(cin>>n>>m){
    if(n==0&&m==0) break;
    int t[m+1];
    for(int i=0;i<=m;i++) t[i] = 0;
    for(int i=0;i<n;i++){
      cin >> k;
      for(int j=0;j<4;j++){
	for(int g=m-1;g>=0;g--){
	  if(t[g]>=1&&g+k<=m&&t[g]<4){
	    if(t[g+k]==0||t[g+k]>t[g]+1) t[g+k] = t[g] + 1;
	  }
	  if(g==0) t[k] = 1;
	}
      }
    }
    for(int i=m;i>=0;i--){
      if(t[i]!=0){
	cout << i << endl;
	break;
      }
    }
  }
  return 0;
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;
   
int32_t N, M, P[1123];
std::vector<int32_t>list;
int32_t ans;
   
int main()
{
    while (true)
    {
        using std::begin;using std::end;

        N = M = 0;
        memset(P, 0, sizeof(P));
        list.clear();
        ans = 0;
   
        in >> N >> M;
        if (N == 0 && M == 0) { break; }
        rep(i, N) { in >> P[i]; }
   
        P[N] = 0;
        N++;
        //std::sort(P, P + N);
        rep(i, N)
        {
            rep(j, N) { list.push_back(P[i] + P[j]); }
        }
        std::sort(begin(list), end(list));
        for (auto& i : list)
        {
            if (i > M) { break; }
            auto iter = upper_bound(begin(list), end(list), M - i);
            --iter;
            ans = std::max(ans, i + *iter);
        }
   
        out << ans << std::endl;
    }
    return 0;
}

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

using ll = long long;
int n, m;

int main() {
    while (true) {
        std::cin >> n >> m;
        if (n == 0 && m == 0) {
            return 0;
        }
        std::vector<int> vec(n);
        for (int i = 0; i < n; i++) {
            std::cin >> vec[i];
        }
        std::vector<int> a;
        for (int i = 0; i < n; i++) {
            a.push_back(vec[i]);
            for (int j = 0; j < n; j++) {
                a.push_back(vec[i] + vec[j]);
            }
        }
        a.push_back(0);

        std::sort(a.begin(), a.end());
        ll ans = 0;
        for (int r = 0; r < a.size(); r++) {
            if (a[r] > m) {
                break;
            }
            auto e = std::lower_bound(a.begin(), a.end(), m - a[r]);
            ll res;
            if (*e == m - a[r]) {
                res = m;
            }
            else {
                e--;
                res = (a[r]) + *e;
            }
            ans = std::max(ans, res);
        }
        std::cout << ans << std::endl;
    }
}

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 ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#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 dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
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;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { 1, -1, 0,  0 };
static const int dy[] = { 0,  0, 1, -1 };

int n, m, ans;
VI ar;
int solve( int now, int d, int cnt ) {
	if ( cnt == 4 || now == n || d + ar[now] > m) {
		return d;
	}
	int res = 0;
	for ( int i = 0; i + cnt <= 4 && d + ar[now] * i <= m; i++ ) {
		res = max( solve( now + 1, d + ar[ now ] * i, cnt + i ), res );
	}
	return res;
}
int main() {
	while ( ~scanf( "%d %d", &n, &m ) && n && m ) {
		ar.resize( n );
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d", &ar[ i ] );
		}
		sort( ar.begin(), ar.end() );
		ans = solve( 0, 0, 0 );
		printf( "%d\n", ans );
	}
	return 0;
}

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

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n, m;
	while (true) {
		cin >> n >> m;
		if (!n && !m) break;
		vector<int> p(n), half(1);
		for (auto& i : p) {
			cin >> i;
			half.push_back(i);
		}
		for (auto i = 0; i < n; i++)
			for (auto j = i; j < n; j++)
				half.push_back(p[i]+p[j]);
		sort(begin(half), end(half));
		int res = 0;
		for (auto& i : half) {
			if (i > m) break;
			auto ideal = m-i;
			auto ub = upper_bound(begin(half), end(half), ideal);
			if (ub != begin(half)) --ub; 
			res = max(res, i+*ub);
		}
		cout << res << endl;
	}
	return 0;
}