#include <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

int N, M;
int P[1000];
vector<int> D;

int main() {
  while (cin >> N >> M) {
    if (N == 0 && M == 0) break;
    for (int i=0; i<N; i++) cin >> P[i];

    D.clear();
    D.push_back(0);
    for (int i=0; i<N; i++) {
      D.push_back(P[i]);
      for (int j=0; j<N; j++) {
        D.push_back(P[i] + P[j]);
      }
    }

    sort(D.begin(), D.end());
    int ans = 0;
    for (auto x : D) {
      int t = x + *(upper_bound(D.begin(), D.end(), M - x) - 1);
      if (t <= M) ans = max(ans, t);
    }
    cout << ans << "\n";
  }
  return 0;
}