#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
	int n,m,t;
	while(cin >> n >> m, n){
		vector<int> data;
		for(int i=0;i<n;i++){
			cin >> t;
			data.push_back(t);
		}
		data.push_back(0);

		
		vector<int> ret;
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++){
				ret.push_back(data[i]+data[j]);
			}
		}
		
		sort(ret.begin(),ret.end());
		
		int r = 0;
		for(int i=0;i<ret.size();i++){
			int t = m - ret[i];
			if( t < 0 )continue;
			r = max(r,ret[i] + *(lower_bound(ret.begin(),ret.end(),t)-1));
		}
		cout << r << endl;
	}
}