alexjercan/bugnet huggingface dataset:
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef int INT;
typedef vector<INT> VI;
typedef set<INT> SI;

INT P[1002];
INT Q[1002001];

INT solve(int N, INT M) {
  INT answer = 0;
  for (int i = 0; i < N; ++i) {
    int q = Q[i];
    int L = i;
    int R = N;
    INT m = M - q;
    while (L < R) {
      int p = (R - L) / 2 + L;
      INT t = q + Q[p];
      if (t <= M)
        answer = max(answer, t);
      if (Q[p] > m) {
        R = p - 1;
      } else {
        L = p + 1;
      }
    }
  }
  return answer;
}

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; ++i) {
      INT p;
      cin >> p;
      P[i] = p;
    }
    P[n] = 0;
    for (int i = 0; i < n + 1; ++i) {
      for (int j = 0; j < n + 1; ++j) {
        Q[i * (n + 1) + j] = P[i] + P[j];
      }
    }
    int length = (n + 1) * (n + 1);
    sort(Q, Q + length);
    cout << solve(length, m) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef int INT;
typedef vector<INT> VI;
typedef set<INT> SI;

INT P[1002];
INT Q[1002001];

INT solve(int N, INT M) {
  INT answer = 0;
  for (int i = 0; i < N; ++i) {
    int q = Q[i];
    int L = 0;
    int R = N;
    INT m = M - q;
    while (L < R) {
      int p = (R - L) / 2 + L;
      INT t = q + Q[p];
      if (t <= M)
        answer = max(answer, t);
      if (Q[p] > m) {
        R = p - 1;
      } else {
        L = p + 1;
      }
    }
  }
  return answer;
}

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; ++i) {
      INT p;
      cin >> p;
      P[i] = p;
    }
    P[n] = 0;
    for (int i = 0; i < n + 1; ++i) {
      for (int j = 0; j < n + 1; ++j) {
        Q[i * (n + 1) + j] = P[i] + P[j];
      }
    }
    int length = (n + 1) * (n + 1);
    sort(Q, Q + length);
    cout << solve(length, m) << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#include "stdio.h"
#include <algorithm>
#include <iostream>

using namespace std;

long long int gten[1111111];
long long int p[1111];

long long int ans[1000];

int main() {
  int ww = 0;
  while (1) {
    long long int n, m;
    scanf("%lld %lld", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    p[0] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &(p[i]));
    }
    int nw = 0;
    for (int i = 0; i <= n; i++) {
      for (int ii = 0; ii <= n; ii++) {
        gten[nw] = p[i] + p[ii];
        nw++;
      }
    }
    sort(gten, gten + nw);
    long long int max = 0;
    int ai = 0;
    for (int i = nw - 1; i >= 0; i--) {
      while (1) {
        if (ai + 1 >= nw) {
          break;
        }
        if (gten[i] + gten[ai + 1] <= m) {
          ai++;
          if (max < gten[i] + gten[ai]) {
            max = gten[i] + gten[ai];
          }
        } else {
          break;
        }
      }
    }
    ans[ww] = max;
    ww++;
  }
  for (int i = 0; i < ww; i++) {
    printf("%lld\\n", ans[i]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include "stdio.h"
#include <algorithm>
#include <iostream>

using namespace std;

long long int gten[1000000];
long long int p[1111];

long long int ans[1000];

int main() {
  int ww = 0;
  while (1) {
    long long int n, m;
    scanf("%lld %lld", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    p[0] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &(p[i]));
    }
    int nw = 0;
    for (int i = 0; i <= n; i++) {
      for (int ii = 0; ii <= n; ii++) {
        gten[nw] = p[i] + p[ii];
        nw++;
      }
    }
    sort(gten, gten + nw);
    long long int max = 0;
    int ai = 0;
    for (int i = nw - 1; i >= 0; i--) {
      while (1) {
        if (ai + 1 >= nw) {
          break;
        }
        if (gten[i] + gten[ai + 1] <= m) {
          ai++;
          if (max < gten[i] + gten[ai]) {
            max = gten[i] + gten[ai];
          }
        } else {
          break;
        }
      }
    }
    ans[ww] = max;
    ww++;
  }
  for (int i = 0; i < ww; i++) {
    printf("%lld\\n", ans[i]);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
long long m;
int n;
long long num[1001];
long long sum[1000005];
int main() {
  while (1) {
    scanf("%d %lld", &n, &m);
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%lld", &num[i]);
    }
    num[n] = 0;
    int ro = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        sum[ro] = num[i] + num[j];
        ro++;
      }
    }
    long long ans = 0;
    sort(sum, sum + ro);
    for (int i = 0; i < ro; i++) {
      if (sum[i] > m) {
        break;
      }
      int rp = lower_bound(sum, sum + ro, m - sum[i]) - sum;
      rp--;
      ans = max(ans, sum[i] + sum[rp]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int n, m;
int num[1001];
long long sum[1000005];
int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
      scanf("%d", &num[i]);
    }
    int ro = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        sum[ro] = num[i] + num[j];
        ro++;
      }
    }
    long long ans = 0;
    sort(sum, sum + ro);
    for (int i = 0; i < ro; i++) {
      if (sum[i] > m) {
        break;
      }
      int rp = lower_bound(sum, sum + ro, m - sum[i]) - sum;
      if (!rp) {
        ans = max(ans, sum[i]);
      } else {
        rp--;
        ans = max(ans, sum[i] + sum[rp]);
      }
    }
    printf("%d\\n", sum);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

int P[1001], mem[1001 * 1001], N, M;
int main() {
loop:
  cin >> N >> M;
  if (!N)
    return 0;
  N++;
  P[0] = 0;
  for (int i = 1; i < N; i++)
    cin >> P[i];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      mem[N * i + j] = P[i] + P[j];
  sort(mem, mem + N * N);

  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    if (mem[i] > M)
      continue;
    ans = max(ans, mem[i] + *(upper_bound(mem, mem + N * N, M - mem[i]) - 1));
  }
  cout << ans << endl;
  goto loop;
}

Error: 0

Fixed submission:

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

int P[1000], mem[1000 * 1000], N, M;
int main() {
loop:
  cin >> N >> M;
  if (!N)
    return 0;
  N++;
  P[0] = 0;
  for (int i = 1; i < N; i++)
    cin >> P[i];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      mem[N * i + j] = P[i] + P[j];
  sort(mem, mem + N * N);

  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    if (mem[i] > M)
      continue;
    ans = max(ans, mem[i] + *(upper_bound(mem, mem + N * N, M - mem[i]) - 1));
  }
  cout << ans << endl;
  goto loop;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

int32_t N, M, P[1123];
std::vector<int32_t> list;
int32_t ans;

int main() {
  while (true) {
    using std::begin;
    using std::end;

    N = M = 0;
    memset(P, 0, sizeof(P));
    list.clear();
    ans = 0;

    in >> N >> M;
    if (N == 0 && M == 0) {
      break;
    }
    rep(i, N) { in >> P[i]; }

    P[N] = 0;
    N++;
    // std::sort(P, P + N);
    rep(i, N) {
      rep(j, N) { list.push_back(P[i] + P[j]); }
    }
    std::sort(begin(list), end(list));
    for (auto &i : list) {
      if (i > M) {
        break;
      }
      auto iter = upper_bound(begin(list), end(list), M - i);
      --iter;
      ans = std::max(ans, i + *iter);
    }

    out << ans << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

int32_t N, M, P[1123];
std::set<int32_t> list;
int32_t ans;

int main() {
  while (true) {
    N = M = 0;
    memset(P, 0, sizeof(P));
    list.clear();
    ans = 0;

    in >> N >> M;
    if (N == 0 && M == 0) {
      break;
    }
    rep(i, N) { in >> P[i]; }

    P[N] = 0;
    N++;
    // std::sort(P, P + N);
    rep(i, N) {
      rep(j, N) { list.insert(P[i] + P[j]); }
    }
    for (auto &i : list) {
      if (i > M) {
        break;
      }
      auto iter = list.upper_bound(M - i);
      --iter;
      ans = std::max(ans, i + *iter);
    }

    out << ans << std::endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int sum[1002100];

int main() {
  int n, d;
  int m[1111];
  while (cin >> n >> d, n) {
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; i++) {
      cin >> m[i];
    }
    m[0] = 0;
    n++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        sum[i * n + j] = m[i] + m[j];
      }
    }
    sort(sum, sum + n * n);

    int res = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        int ab = m[i] + m[j];
        if (ab <= d) {
          int h = d - ab;
          int tmp = sum[upper_bound(sum, sum + n * n, h) - sum - 1] + ab;
          if (tmp <= d) {
            res = max(res, tmp);
          }
        }
      }
    }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int sum[1001100];

int main() {
  int n, d;
  int m[1111];
  while (cin >> n >> d, n) {
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; i++) {
      cin >> m[i];
    }
    m[0] = 0;
    n++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        sum[i * n + j] = m[i] + m[j];
      }
    }
    sort(sum, sum + n * n);

    int res = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        int ab = m[i] + m[j];
        if (ab <= d) {
          int h = d - ab;
          int tmp = sum[upper_bound(sum, sum + n * n, h) - sum - 1] + ab;
          if (tmp <= d) {
            res = max(res, tmp);
          }
        }
      }
    }
    cout << res << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M && (N || M)) {
    int P[1001] = {0}, ans = 0;
    for (int i = 0; i < N; ++i) {
      scanf("%d", &P[i]);
    }
    vector<int> v;
    for (int i = 0; i <= N; ++i) {
      for (int j = 0; j <= N; ++j) {
        if (P[i] + P[j] <= M)
          v.push_back(P[i] + P[j]);
      }
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    for (unsigned 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]);
      if (it == v.end())
        --it;
      if (*it > M - v[i])
        --it;

      ans = max(ans, v[i] + *it);
    }
    /*for(int i = 0; i <= N; ++i){
            for(int j = 0; j <= N; ++j){
                    if(P[i] + P[j] > M)
                            continue;
                    set<int>::iterator it = darts.lower_bound(M - (P[i] +
    P[j])); if(it == darts.end())
                            --it;
                    if(*it > M - (P[i] + P[j]))
                            --it;

                    ans = max(ans, P[i] + P[j] + *it);
            }
    }*/
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <string.h>
#include <vector>
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M && (N || M)) {
    int P[1001] = {0}, ans = 0;
    for (int i = 0; i < N; ++i) {
      scanf("%d", &P[i]);
    }
    set<int> darts;
    for (int i = 0; i <= N; ++i) {
      for (int j = 0; j <= N; ++j) {
        if (P[i] + P[j] <= M)
          darts.insert(darts.end(), P[i] + P[j]);
      }
    }
    vector<int> v(darts.begin(), darts.end());
    for (unsigned 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]);
      if (it == v.end())
        --it;
      if (*it > M - v[i])
        --it;

      ans = max(ans, v[i] + *it);
    }
    /*for(int i = 0; i <= N; ++i){
            for(int j = 0; j <= N; ++j){
                    if(P[i] + P[j] > M)
                            continue;
                    set<int>::iterator it = darts.lower_bound(M - (P[i] +
    P[j])); if(it == darts.end())
                            --it;
                    if(*it > M - (P[i] + P[j]))
                            --it;

                    ans = max(ans, P[i] + P[j] + *it);
            }
    }*/
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define loop(i, n) for (int i = 0; i < n; i++)
int n, m;
int p[1002];
int pp[1111111];

signed main() {
  while (cin >> n >> m, n) {
    loop(i, n) cin >> p[i];
    p[n++] = 0;
    loop(i, n) loop(j, n) { pp[i * n + j] = m - p[i] - p[j]; }
    sort(pp, pp + n * n);
    int res = 0;
    loop(i, n) loop(j, n) {
      int s = p[i] + p[j];
      int t = *lower_bound(pp, pp + n * n, s);
      if (t > 0)
        res = max(res, s - t + m);
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define loop(i, n) for (int i = 1; i <= n; i++)
int n, m;
int p[1002];
set<int> s1;
set<int> s2;

signed main() {
  while (cin >> n >> m, n) {
    loop(i, n) cin >> p[i];
    s1.clear();
    s2.clear();
    s1.insert(0);
    loop(a, 4) {
      for (int x : s1) {
        s2.insert(x);
        loop(i, n) { s2.insert(x + p[i]); }
      }
      swap(s1, s2);
    }
    int mx = 0;
    for (int x : s1) {
      if (x <= m)
        mx = max(x, mx);
    }
    cout << mx << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, m, p[1001], q[1002001], t, k, ans;
int main() {
  while (cin >> n >> m, n) {
    rep(i, n) cin >> t, p[i + 1] = -t;
    ans = 0, k = (n + 1) * (n + 1);
    rep(i, n + 1) rep(j, n + 1) q[i * n + j] = p[i] + p[j];
    sort(q, q + k);
    rep(i, k) if (q[i] >= -m) {
      int *j = lower_bound(q, q + k, -m - q[i]);
      if (ans > q[i] + *j)
        ans = q[i] + *j;
    }
    cout << -ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, m, p[1001], ans;
int main() {
  while (cin >> n >> m, n) {
    rep(i, n) cin >> p[i + 1];
    ans = 0;
    set<int> q;
    rep(i, n + 1) rep(j, n + 1) q.insert(p[i] + p[j]);
    for (set<int>::iterator i = q.begin(), j; i != q.end() && *i <= m; i++) {
      j = q.upper_bound(m - *i);
      if (*j != m - *i)
        j--;
      if (ans < *i + *j)
        ans = *i + *j;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m, a[1001], t, p, q, r;
  bool f;

  while (cin >> n >> m && n && m) {
    vector<int> vec;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    a[0] = 0;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        r = a[i] + a[j];
        f = true;
        for (int k = 0; k < vec.size(); k++) {
          if (r == vec[i]) {
            f = false;
            break;
          }
        }
        if (f) {
          vec.push_back(r);
        }
      }
    }
    sort(vec.begin(), vec.end());

    t = 0;
    for (int i = 0; i < vec.size(); i++) {
      p = 0;
      q = vec.size();
      while (q - p > 1) {
        r = (p + q) / 2;
        if (vec[i] + vec[r] >= m) {
          q = r;
        } else {
          p = r;
        }
      }
      r = vec[i] + vec[p];
      if (t < r && r <= m) {
        t = r;
      }
    }

    cout << t << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>

using namespace std;

int main() {
  int n, m, a[1001], s, t;

  while (cin >> n >> m && n && m) {
    set<int> st;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    a[0] = 0;
    t = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        st.insert(a[i] + a[j]);
      }
    }

    bool f = true;
    for (set<int>::iterator it1 = st.begin(); it1 != st.end() && f; it1++) {
      for (set<int>::iterator it2 = st.begin(); it2 != st.end(); it2++) {
        s = (*it1) + (*it2);
        if (t < s && s <= m) {
          t = s;
          if (t == m) {
            f = false;
            break;
          }
        }
      }
    }

    cout << t << endl;
  }

  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (!N && !M)
      break;

    vector<int> points;
    vector<int> points_of_two;
    for (int i = 0; i < N; ++i) {
      int p;
      cin >> p;
      points.push_back(p);
    }
    for (int i = 0; i < N; ++i) {
      if (points[i] <= M)
        points_of_two.push_back(points[i]);
      for (int j = 0; j < i; ++j) {
        int sum = points[i] + points[j];
        if (sum <= M)
          points_of_two.push_back(sum);
      }
    }
    points_of_two.push_back(0);
    sort(points_of_two.begin(), points_of_two.end());
    points_of_two.erase(unique(points_of_two.begin(), points_of_two.end()),
                        points_of_two.end());

    int ans = 0;
    for (int i = 0; i < points_of_two.size(); ++i) {
      int rem = M - points_of_two[i];
      int left = 0, right = points_of_two.size();
      while (left < right) {
        int center = (left + right) / 2;
        if (points_of_two[center] <= rem) {
          left = center + 1;
        } else {
          right = center;
        }
      }
      if (left != 0)
        --left;
      int score = points_of_two[i] + points_of_two[left];
      if (score > ans)
        ans = score;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (!N && !M)
      break;

    vector<int> points;
    set<int> points_of_two;
    for (int i = 0; i < N; ++i) {
      int p;
      cin >> p;
      points.push_back(p);
    }
    for (int i = 0; i < N; ++i) {
      if (points[i] <= M)
        points_of_two.insert(points[i]);
      for (int j = 0; j < i; ++j) {
        int sum = points[i] + points[j];
        if (sum <= M)
          points_of_two.insert(sum);
      }
    }
    points_of_two.insert(0);

    int ans = 0;
    for (set<int>::iterator it = points_of_two.begin();
         it != points_of_two.end(); ++it) {
      int rem = M - *it;
      set<int>::iterator bound = points_of_two.upper_bound(rem);
      if (bound != points_of_two.begin())
        --bound;
      int score = *it + *bound;
      if (score > ans)
        ans = score;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

int n, m;
vector<int> p1, p2;
int main() {
  while (cin >> n >> m && n && m) {
    p1.clear();
    p2.clear();

    // chap1
    p1.push_back(0);
    for (int i = 0; i < n; i++) {
      int p;
      cin >> p;
      p1.push_back(p);
    }

    // chap2
    for (int i = 0; i <= n; i++)
      for (int j = i; j <= n; j++)
        p2.push_back(p1[i] + p1[j]);

    sort(p2.begin(), p2.begin() + p2.size());

    // chap3
    int ans = 0;
    for (int i = 0; i < p2.size(); i++) {
      vector<int>::iterator count2 =
          lower_bound(p2.begin(), p2.end(), m - p2[i]);
      if (count2 != p2.begin())
        ans = max(ans, *(count2 - 1) + p2[i]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int n, m;
int p[1001], dp[5][1001];
int main() {
  while (true) {
    // chap1
    cin >> n >> m;
    if (!n && !m)
      break;

    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; i++)
      cin >> p[i];

    // chap2
    for (int i = 1; i <= n; i++)
      for (int j = 0; j <= 4; j++)
        for (int k = 0; j + k <= 4; k++)
          if (dp[i - 1][j] + p[i] * k <= m)
            dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] + p[i] * k);

    cout << dp[n][4] << endl;
  }
  return 0;
}