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+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 <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define Rep(i, n) for (long long i = 0; i< (long long)(n); i++)
#define RRep(i,n) for (long long i = ((long long)(n)-1); i>=0; i--)

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;

template <class T>
inline bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

ll INF = 1e9+7;

vector<int> dx = {-1,0,1,0};
vector<int> dy = {0,-1,0,1};

template<class Abel> struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<Abel> diff_weight;

    UnionFind(int n = 1, Abel SUM_UNITY = 0) {
        init(n, SUM_UNITY);
    }

    void init(int n = 1, Abel SUM_UNITY = 0) {
        par.resize(n); rank.resize(n); diff_weight.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            diff_weight[x] += diff_weight[par[x]];
            return par[x] = r;
        }
    }

    Abel weight(int x) {
        root(x);
        return diff_weight[x];
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y, Abel w) {
        w += weight(x); w -= weight(y);
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y), w = -w;
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        diff_weight[y] = w;
        return true;
    }

    Abel diff(int x, int y) {
        return weight(y) - weight(x);
    }
};

int solve(){
    ll n,m;
    cin >> n >> m;
    if(n==0 && m == 0) return 1;
    vector<ll> p(n);
    Rep(i,n) cin >> p[i];
    Rep(i,n) Rep(j,n){
        p.emplace_back(p[i]+p[j]);
    }
    sort(p.begin(),p.end());
    ll ans = 0;
    Rep(i,p.size()){
        ll x = m-p[i];
        if(x<0) break;
        auto y = lower_bound(p.begin(),p.end(),x);
        if(y-p.begin()==0) chmax(ans,p[i]);
        else{
            chmax(ans,p[i]+p[y-p.begin()-1]);
        }
    }
    std::cout << ans << endl;
    return 0;
}

int main(){
    int a = 0;
    while(a==0){
        a = solve();
    }
}

C++: #include <cstdio>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n, m, c[1009], s[1000009];
int main() {
	while (scanf("%d%d", &n, &m), n) {
		for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
		int b = 0;
		for (int i = 0; i <= n; i++) {
			for (int j = i; j <= n; j++) {
				if (c[i] + c[j] <= m) s[b++] = c[i] + c[j];
				else break;
			}
		}
		sort(s, s + b);
		int r = b - 1, ret = 0;
		for (int i = 0; i < b && i <= r; i++) {
			while (r > 0 && s[i] + s[r] > m) r--;
			ret = max(ret, s[i] + s[r]);
		}
		printf("%d\n", ret);
	}
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int N,M,P[1000];
int main() {
	while(scanf("%d %d",&N,&M),N|M) {
		for(int i=0;i<N;i++) scanf("%d",&P[i]);
		int ans=0;
		vector<int> X,Y;
		X.push_back(0);
		for(int i=0;i<4;i++) {
			for(int j=0;j<X.size();j++) {
				for(int k=0;k<N;k++) {
					int v=(X[j]+P[k])%M;
					ans=max(ans,v);
					Y.push_back(v);
				}
			}
			Y.erase(unique(Y.begin(),Y.end()),Y.end());
			X=Y;
		}
		printf("%d\n",ans);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n,m;
  while(cin>>n>>m,n){
    int p[n];
    for(int i=0;i<n;i++) cin>>p[i];
    vector<int> s;
    s.emplace_back(0);
    s.emplace_back(m+1);
    for(int i=0;i<n;i++)
      s.emplace_back(p[i]);
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	s.emplace_back(p[i]+p[j]);
    sort(s.begin(),s.end());
    s.erase(unique(s.begin(),s.end()),s.end());
    int ans=0;
    auto latte=--s.end();
    for(int x:s){
      if(x>m) continue;
      while(latte!=s.begin()&&x+*latte>m) --latte;
      int y=*latte;
      if(x+y>m) continue;
      ans=max(ans,x+y);
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#define INF 1<<28
using namespace std;

int p[1001];
int n;

int solve(int s, int m){

  if(s < 0) return INF;
  if(!m) return s;

  int res = INF;
  for(int i=0;i<=n;i++) res = min(res, solve(s-p[i], m-1));

  return res;
}

int main(){

  int s;
  while(true){
    cin >> n >> s;
    if(!n && !s) break;

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

    cout << s-solve(s, 4) << endl;

  }
}

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

using namespace std;

int main() {
    while (true) {
        int n, m;
        cin >> n >> m;
        if (n == 0 && m == 0) return 0;
        int p[n + 1];
        p[0] = 0;
        for (int i = 0; i < n; i++) {
            cin >> p[i + 1];
        }
        sort(p, p + (n + 1));
        int *bound = upper_bound(p, p + (n + 1), m);
        int result = 0;
        for (int *ptr = bound - 1; ptr >= p; ptr--) {
            int a = *ptr;
            int *bound2 = upper_bound(p, bound, m - a);
            for (int *ptr2 = bound2 - 1; ptr2 >= p; ptr2--) {
                int b = *ptr2;
                int *bound3 = upper_bound(p, bound2, m - a - b);
                for (int *ptr3 = bound3 - 1; ptr3 >= p; ptr3--) {
                    int c = *ptr3;
                    int *bound4 = upper_bound(p, bound3, m - a - b - c);
                    if ((bound4 - 1) < p) continue;
                    int d = *(bound4 - 1);
                    result = max(result, a + b + c + d);
                }
            }
        }
        cout << result << endl;
    }
}