#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){

    int N,M;
    while(cin>>N>>M,N){
        int p[1200];
        p[0] = 0;
        for(int i = 1;i <= N;i++)cin>>p[i];
        vector<int> V;
        for(int i = 0;i <= N;i++){
            for(int j = 0;j <= N;j++){
                V.push_back(p[i]+p[j]);
            }
        }
        sort(V.begin(),V.end());
        int ans = 0;
        for(int i = 0;i < V.size();i++){
            if(V[i]>M)continue;
            vector<int>::iterator it = lower_bound(V.begin(),V.end(),M-V[i]);
            it--;
            //cout<<V[i]<<" "<<*it<<endl;
            ans = max(ans,V[i] + *it);
        }
        cout<<ans<<endl;
    }

    return 0;
}