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

int main()
{
	int n,m,in,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&in);
			P.push_back(in);
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			if(P[i] + *it >= m){
				MAX = max(MAX,P[i] + *it);
			}
		}
	
		printf("%d\n",MAX);
	}
	
	return 0;
}

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

using namespace std;

int main()
{
	int n, m, a[1001], s, t;
	
	while(cin>>n>>m&&n&&m){
		set<int> st;
		for(int i = 1; i <= n; i++){
			cin >> a[i];
		}
		a[0] = 0;
		t = 0;
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= n; j++){
				st.insert(a[i]+a[j]);
			}
		}
		
		bool f = true;
		for(set<int>::iterator it1 = st.begin(); it1 != st.end() && f; it1++){
			for(set<int>::iterator it2 = st.begin(); it2 != st.end(); it2++){
				s = (*it1) + (*it2);
				if(t <  s&& s <= m){
					t = s;
					if(t == m){
						f = false;
						break;
					}
				}
			}
		}
		
		cout << t << endl;
	}
	
	return 0;
}

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

using namespace std;

int main()
{
	int N, M, D;

	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

		if (N == 0 && M == 0) { break; }

		vector<int> C; C.push_back(0);

		for (int i = 0; i < N; i++)
		{
			scanf("%d", &D);

			if (D <= M)
			{
				C.push_back(D);
			}
		}

		vector<int> r;

		for (int i = 0; i < C.size(); i++)
		{
			for (int j = i; j < C.size(); j++)
			{
				if (C[i] + C[j] <= M)
				{
					r.push_back(C[i] + C[j]);
				}
			}
		}

		sort(r.begin(), r.end());

		int ret = 0, lc = 0, rc = r.size() - 1;

		while (rc - lc >= 0)
		{
			if (r[lc] + r[rc] > M)
			{
				rc--;
			}
			else
			{
				ret = max(ret, r[lc++] + r[rc]);
			}
		}

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

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n >> m && n && m){
    vary(ll,s,n,0);
    rep(i,n){
      cin >> s[i];
    }
    sort(all(s));
    vector<vector<ll>> dp(n+2,vector<ll>(m+2,inf));
    rep(i,m+1){
      dp[0][i] = 0;
    }
    rep(i,n+1){
      dp[i][0] = 0;
    }
    REP(i,0,n){
      for(ll j = 0; j < m+1 + s[i]; j += s[i]){
        if(j - s[i] >= 0){
          dp[i+1][j] = min(dp[i+1][j],dp[i][j-s[i]]+1);
        }
      }
      if(i > 0){
        rep(j,m+1){
          dp[i+1][j] = min(dp[i][j],dp[i+1][j]);
        }
      }
    }
    ll ans = 0;
    for(ll j = n; j >= 0; --j){
      for(ll i = m; i >= 0; --i){
        if(dp[j][i] != inf && dp[j][i] != 0 && dp[j][i] <= m){
          ans = i;
          j = -1;
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll MAX_N = ll(1e5 + 5);
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i,n) for (int i = n; i >=0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()


int main() {
    int n, m, ans, tmp;
    vector<int> p, sum;
    while(1){
        cin >> n >> m;
        if(n==0)break;
        p.resize(n);
        sum.clear();
        REP(i,n)cin >> p[i];
        SORT(p);
        for(int i = 0; i<n; i++){
            if(p[i] > m)break;
            sum.push_back(p[i]);
            if(p[i] > m/2)continue;
            for(int j = i; j<n; j++){
                if(p[i]+p[j]>m)break;
                sum.push_back(p[i]+p[j]);
            }
            
        }
        sum.push_back(0);
        SORT(sum);
        ans = 0;
        for(auto& itr : sum){
            if(itr > m/2)break;
            tmp = itr + *--upper_bound(ALL(sum),m-itr);
            if(tmp > m)continue;
            ans = max(ans, tmp);
            if(ans == m)break;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const long long inf = 8000000000000000;
struct P {
	ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b. cost; }
bool operator>(P a, P b) { return a.cost > b. cost; }
int main() {
	ll a[1000], n, m;
	while (cin >> n >> m&&n != 0 && m != 0) {
		for (int i = 0; i < n; i++) cin >> a[i];
		ll b[1000];
		fill(b, b + n, -1);
		b[0] = a[0];
		priority_queue<P, vector<P>, greater<P>>p;
		p.push(P{ 0,a[0] });
		while (!p.empty()) {
			P t = p.top(); p.pop();
			for (int i = 0; i < n; i++) {
				if (a[i] + b[t.pos] > b[i] && a[i] + b[t.pos] <= m) {
					b[i] = a[i] + b[t.pos];
					p.push(P{ i,b[i] });
				}
			}
		}
		cout << max(b[n - 1],(ll)0) << endl;
	}
}