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

int main(){
  int n,m;
  int p[1001];
  int s[1001000];
  for(;;){
    cin >> n >> m;
    if(!n && !m)break;

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

    for(int i=0;i<=n;i++)
      for(int j=0;j<=n;j++)s[i*(n+1)+j] = p[i] + p[j];

    int N = (n+1)*(n+1);
    sort(s,s+N);
      
    int ans = 0;
    for(int i=0;i<N;i++){
      int hoge = upper_bound(s,s+N,m-s[i]) - s;
      //cout << s[i] << " " << s[hoge-1] << endl;
      if(hoge == 0)break;
      else if(hoge == N)ans = max(ans,s[i]);
      else ans = max(ans,s[i] + s[hoge-1] );
    }

    cout << ans << endl;
  }
}
	  