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 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;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
int main(){
while(1){
int i,n,j,m,s=0,l,k,o=0;
static int p[1001]={0},u[1001][1001]={0},v[1001*1001];
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++)scanf("%d",&p[i]);
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
if(u[i][j]==0&&u[j][i]==0){
v[o]=p[i]+p[j];
o++;
}
}
}
for(i=0;i<o;i++){
for(j=0;j<o;j++){
if(v[i]+v[j]<=m&&v[i]+v[j]>s)s=v[i]+v[j];
}
}
printf("%d\n",s);
}
return 0;
}

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

int main(){
  int n,m;
  int p[1001];
  for(;;){
    cin >> n >> m;
    if(!n && !m)break;

    for(int i=0;i<n;i++)cin >> p[i];
    p[n] = 0;

    set<int> s;
    for(int i=0;i<=n;i++)
      for(int j=0;j<=n;j++)s.insert(p[i] + p[j]);

    set<int>::iterator ite,hoge;
   
    int ans = 0;
    for(ite = s.begin();ite!=s.end();ite++){
      int tmp = *ite;
      hoge = upper_bound(s.begin(),s.end(),m-tmp);
      if(hoge == s.begin())break;
      else if(hoge == s.end())ans = max(ans,tmp);
      else ans = max(ans,tmp + (*(--hoge) ) );
    }

    cout << ans << endl;
  }
}

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

int main(){
  int N, M;
  int P[1001], SUM[1000001];
  P[0] = 0;
  while(cin >> N >> M , N){

    for(int i = 1 ; i <= N ; i++ ){
      cin >> P[i];
    }
    int cnt = 0;
    for(int i = 0 ; i <= N ; i++ ){
      for(int j = N ; j >= i ; j-- ){
        if(P[i] + P[j] > M) continue;
        SUM[cnt++] = P[i] + P[j];
      }
    }
    sort( SUM, SUM + cnt);

   int ret = 0;
    for(int i = 0 ; i < cnt ; i++ ){
      ret = max( ret, SUM[i] + *(upper_bound( SUM, SUM + cnt, M - SUM[i]) - 1));
    }
    cout << ret << endl;
  }
}

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		long long int b;
		set<long long int> ss;
		ss.insert(0);
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				ss.insert(b);
			}
		}
		set<long long int> ss2;
		for (set<long long int>::iterator ite = ss.begin(); ite != ss.end(); ite++) {
			ss2.insert(*(ite));
			for (set<long long int>::iterator ite2 = ite; ite2 != ss.end(); ite2++) {
				if (*(ite)+*(ite2) <= m) {
					ss2.insert(*(ite)+*(ite2));
				} else {
					break;
				}
			}
		}
		ss.clear();
		vector<long long int> data;
		for (set<long long int>::iterator ite = ss2.begin(); ite != ss2.end(); ite++) {
			data.push_back(*(ite));
		}
		//cout << 'a' << endl;
		long long int a = m;
		for (long long int i = 0; i < 4; i++) {
			vector<long long int>::iterator ite = upper_bound(data.begin(), data.end(), a);
			ite--;
			a -= *(ite);
		}
		cout << m-a << endl;
	}
	return 0;
}