Darts (AOJ 0529) by letter
Contest: 3575    RunID: 2376055    Status: Compile Error    Date: Sat Jun 17 14:49:13 JST 2017


#include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
 
typedef long long ll;
typedef pair<int, int> ii;

#define DEBUG false

int main(){

	ll n, m;
	while(cin >> n >> m){
		if(n == 0 && m == 0) break;
		vector<ll> p(n+1);
		rep(i, n) cin >> p[i];
		
		rep(i, n){
			REP(j, i, n){
				p.push_back(p[i] + p[j]);
			}
		}
		
		SORT(p);
		
		ll ans = 0;

		rep(i, p.size()){
			auto it2 = upper_bound(p.begin(), p.end(), m - p[i]);
			if(it2 == p.begin()) continue;
			it2--;
			if(p[i] > *it2) break;
			ans = max(ans, p[i] + *it2);
		}
		
		cout << ans << endl;
		
	}
	
	return 0;
}