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

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

int binary(vector<int> &a, int tar) {
  int begin = 0, end = a.size() - 1, mid = (begin + end) / 2;
  while (begin < mid) {
    if (a[mid] < tar) {
      begin = mid;
    } else {
      end = mid;
    }
    mid = (begin + end) / 2;
  }
  return a[mid];
}

int main(int argc, char *argv[]) {
  int n, m;
  while (scanf("%d %d\\n", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    int point[n + 1];
    point[0] = 0;
    vector<int> a;
    for (int i = 1; i <= n; i++) {
      scanf("%d\\n", &point[i]);
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a.push_back(point[i] + point[j]);
      }
    }
    sort(a.begin(), a.end());
    int ans = 0;
    for (int i : a) {
      int tmp = binary(a, m - i);
      if (i + tmp <= m)
        ans = max(ans, i + tmp);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(int argc, char *argv[]) {
  int n, m;
  while (scanf("%d %d\\n", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    int point[n + 1];
    point[0] = 0;
    set<int> a;
    for (int i = 1; i <= n; i++) {
      scanf("%d\\n", &point[i]);
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a.insert(point[i] + point[j]);
      }
    }
    int ans = 0;
    for (int i : a) {
      int tmp = *(--a.upper_bound(m - i));
      if (i + tmp <= m)
        ans = max(ans, i + tmp);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int max_sum(int a[], int as, int b[], int bs, int m) {
  int j = bs - 1;
  int res = 0;
  for (int i = 0; i < as; i++) {
    while (0 <= j && m < a[i] + b[j])
      j--;
    if (j == -1)
      break;
    res = max(res, a[i] + b[j]);
  }
  return res;
}

int p[1000], p2[1000 * 1001 / 2];
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> p[i];
      if (p[i] <= m)
        ans = max(ans, p[i]);
    }
    sort(p, p + n);
    int n2 = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        int a = p[i] + p[j];
        p2[n2++] = a;
        if (a <= m)
          ans = max(ans, a);
      }
    }
    sort(p2, p2 + n2);
    ans = max(ans, max_sum(p, n, p2, n2, m));
    ans = max(ans, max_sum(p2, n2, p2, n2, m));
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
/*bool ok(int *q,int m,int h,int count){
  for(int i=0;i<count;i++){
    if(q[i]>m) continue;
    for(int j=0;j<count;j++){
      if(q[i]+q[j]<=m&&q[i]+q[j]>h) return true;
    }
  }
  return false;
  }*/
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      return 0;
    int data[n + 1];
    int q[(n + 1) * (n + 1) / 2];
    int ans = 0;
    for (int i = 0; i < n; i++)
      cin >> data[i];
    data[n] = 0;
    int count = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        if (data[i] + data[j] <= m)
          q[count] = data[i] + data[j];
        count++;
      }
    }
    // sort(q,q+count);
    for (int i = 0; i < count; i++) {
      for (int j = 0; j < count; j++)
        if (q[i] + q[j] > ans && q[i] + q[j] <= m)
          ans = q[i] + q[j];
    }
    cout << ans << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
/*bool ok(int *q,int m,int h,int count){
  for(int i=0;i<count;i++){
    if(q[i]>m) continue;
    for(int j=0;j<count;j++){
      if(q[i]+q[j]<=m&&q[i]+q[j]>h) return true;
    }
  }
  return false;
  }*/
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      return 0;
    int data[n + 1];
    int q[(n + 1) * (n + 1) / 2];
    int ans = 0;
    for (int i = 0; i < n; i++)
      cin >> data[i];
    data[n] = 0;
    int count = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        if (data[i] + data[j] > m)
          continue;
        q[count] = data[i] + data[j];
        count++;
      }
    }
    sort(q, q + count);
    int j = count - 1;
    for (int i = 0; i < count; i++) {
      while (0 <= j && m < q[i] + q[j])
        j--;
      if (j == -1)
        break;
      ans = max(ans, q[i] + q[j]);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
/*bool ok(int *q,int m,int h,int count){
  for(int i=0;i<count;i++){
    if(q[i]>m) continue;
    for(int j=0;j<count;j++){
      if(q[i]+q[j]<=m&&q[i]+q[j]>h) return true;
    }
  }
  return false;
  }*/
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      return 0;
    int data[n + 1];
    int q[(n + 1) * (n + 1) / 2];
    int ans = 0;
    for (int i = 0; i < n; i++)
      cin >> data[i];
    data[n] = 0;
    int count = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        if (data[i] + data[j] > m)
          continue;
        q[count] = data[i] + data[j];
        count++;
      }
    }
    for (int i = 0; i < count; i++) {
      for (int j = 0; j < count; j++) {
        if (q[i] + q[j] > m)
          continue;
        ans = max(ans, q[i] + q[j]);
      }
    }
    // sort(q,q+count);
    cout << ans << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int int64;

vector<int64> P, Q;
int64 N, M, T;

int main(void) {
  while (true) {
    N = 0;
    M = 0;
    P.clear();
    Q.clear();
    P.shrink_to_fit();
    Q.shrink_to_fit();
    cin >> N >> M;
    if (N == 0 && M == 0) {
      exit(0);
    }
    for (int i = 0; i < N; i++) {
      cin >> T;
      P.push_back(T);
    }
    P.push_back(0);
    for (auto itr = P.begin(); itr < P.end(); itr++) {
      for (auto itr2 = P.begin(); itr2 < P.end(); itr2++) {
        Q.push_back(*itr + *itr2);
      }
    }
    Q.push_back(LLONG_MAX / 2);
    Q.push_back(LLONG_MIN / 2);
    sort(Q.begin(), Q.end());
    int64 ms = 0;
    for (auto itr = Q.begin(); itr < Q.end() + 1; itr++) {
      ms = max(ms,
               *itr + *(lower_bound(Q.begin(), Q.end() - 1, M - *itr + 1) - 1));
    }
    cout << ms << endl;
  }
}

Error: MLE

Fixed submission:

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

#define MAX_N 1000

using namespace std;

typedef long long int int64;

int64 N, M, P[MAX_N];
vector<int64> T, S;

int main(void) {
  while (true) {
    for (int i = 0; i < MAX_N; i++)
      P[i] = 0;
    cin >> N >> M;
    if (N == 0 && M == 0)
      exit(0);
    for (int i = 0; i < N; i++)
      cin >> P[i];
    T.push_back(0);
    for (int i = 0; i < N; i++) {
      for (int ii = 0; ii < N; ii++)
        T.push_back(P[i] + P[ii]);
    }
    sort(T.begin(), T.end());
    int64 prev = -1;
    for (int i = 0; i < N * N; i++) {
      if (prev != T[i]) {
        S.push_back(T[i]);
        prev = T[i];
      }
    }
    sort(S.begin(), S.end());
    int64 maxscore = 0;
    int64 sc = 0;
    for (auto itr = S.begin(); itr < S.end(); itr++) {
      sc = *(upper_bound(S.begin(), S.end() - 1, M - (*itr)) - 1) + *itr;
      maxscore = max(maxscore, sc);
    }
    cout << maxscore << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

int main() {
  while (true) {
    int N, M, mex;
    mex = -1;
    cin >> N >> M;
    if (N + M == 0)
      break;
    vector<int> val, dp;
    for (int i = 0; i < N; i++) {
      int a;
      cin >> a;
      val.push_back(a);
      dp.push_back(a);
    }
    dp.push_back(0);
    map<int, bool> exist;
    for (int i = 0; i < val.size(); i++) {
      for (int j = 0; j < val.size(); j++) {
        // if(M > val[i]+val[j])continue;
        dp.push_back(val[i] + val[j]);
      }
    }
    sort(dp.begin(), dp.end());
    int mmex = -1;
    for (int i = 0; i < dp.size(); i++) {
      if (M >= dp[i])
        mmex =
            max(mmex, dp[i] + upper_bound(dp.begin(), dp.end(), M - dp[i])[-1]);
    }

    cout << mmex << endl;
    mex = -1;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  int N, M;
  vector<int> vec[5];

  while (true) {
    cin >> N >> M;
    if (N + M == 0)
      break;
    int val[N];
    for (int i = 0; i < 5; i++)
      vec[i].clear();

    int mex = -1;
    int master = -1;

    for (int i = 0; i < N; i++) {
      cin >> val[i];
      mex = max(mex, val[i]);
      vec[1].push_back(val[i]);
    }
    master = mex;
    if (mex == M) {
      cout << M << endl;
      continue;
    }
    bool fin = false;
    for (int i = 2; i <= 4; i++) {
      if (i > 2)
        vec[i - 2].clear();
      for (int j = 0; j < vec[i - 1].size(); j++) {
        if (vec[i - 1][j] + master <= mex)
          continue;
        for (int k = 0; k < N; k++) {
          if (val[k] >= M)
            continue;

          if (i == 4 && vec[i - 1][j] + val[k] <= mex)
            continue;

          vec[i].push_back(vec[i - 1][j] + val[k]);

          if (M >= vec[i - 1][j] + val[k])
            mex = max(mex, vec[i - 1][j] + val[k]);

          if (mex == M) {
            fin = true;
            break;
          }
        }
        if (fin)
          break;
      }
      if (fin)
        break;
    }
    assert(mex != -1);
    cout << mex << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
int i, j;
int gou[6000000];
using namespace std;
int main(void) {
  int n, m;
  int data[1001];
  scanf("%d %d", &n, &m);
  while (n != 0 && m != 0) {
    for (i = 0; i < n; i++) {
      scanf("%d", &data[i]);
    }
    data[n] = 0;
    sort(data, data + n + 1);
    /*for(i=0;i<=n;i++){
            printf("%d \\n",data[i]);
    }*/
    int cnt = 0;
    for (i = 0; i <= n; i++) {
      for (j = i; j <= n; j++) {
        gou[cnt] = data[i] + data[j];
        cnt++;
      }
    }
    sort(gou, gou + cnt);
    /*for(i=0;i<cnt;i++){
            printf("%d ",gou[i]);
    }
    printf("\\n");*/
    //
    int max = 0;
    for (i = 0; i < cnt; i++) {
      int syou = 0, dai = cnt, sach;
      sach = m - gou[i];
      while (dai >= syou) {
        int b;
        b = (dai + syou) / 2;
        if (sach == gou[b]) {
          max = m;
          break;
        } else if (sach > gou[b]) {
          syou = b + 1;
        } else if (sach < gou[b]) {
          dai = b - 1;
        }
      }
      if (max < gou[dai] + gou[i] && m >= gou[dai] + gou[i]) {
        max = gou[dai] + gou[i];
      }
    }
    printf("%d\\n", max);
    scanf("%d %d", &n, &m);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int i, j;
int main(void) {
  int n, m;
  int data[1003];
  scanf("%d %d", &n, &m);
  while (n != 0 && m != 0) {
    for (i = 0; i < n; i++) {
      scanf("%d", &data[i]);
    }
    data[n] = 0;
    sort(data, data + n + 1);
    /*for(i=0;i<=n;i++){
            printf("%d \\n",data[i]);
    }*/
    int gou[500505];
    int cnt = 0;
    for (i = 0; i <= n; i++) {
      for (j = i; j <= n; j++) {
        gou[cnt] = data[i] + data[j];
        cnt++;
      }
    }
    sort(gou, gou + cnt);
    /*for(i=0;i<cnt;i++){
            printf("%d ",gou[i]);
    }
    printf("\\n");*/
    //
    int max = 0;
    for (i = 0; i < cnt; i++) {
      for (j = i; j < cnt; j++) {
        if (m < gou[i] + gou[j]) {
          break;
        } else if (max < gou[i] + gou[j]) {
          max = gou[i] + gou[j];
        }
      }
    }
    printf("%d\\n", max);
    scanf("%d %d", &n, &m);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * File    : JOI_2007_darts.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-3-8 20:45:20
 * Powered by Visual Studio Code
 */

#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <iomanip> // << fixed << setprecision(xxx)
#include <iostream>
#include <map> // if (M.find(key) != M.end()) { }
#include <queue>
#include <random> // random_device rd; mt19937 mt(rd());
#include <set>
#include <stack>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <tuple>
#include <vector>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
int P[1010];
vector<int> S;

int main() {
  while (cin >> N >> M && N && M) {
    S.clear();
    for (auto i = 0; i < N; i++) {
      cin >> P[i];
    }
    P[N] = 0;
    N++;
    for (auto i = 0; i < N; i++) {
      for (auto j = i; j < N; j++) {
        int sum = P[i] + P[j];
        if (sum <= M) {
          S.push_back(sum);
        }
      }
    }
    sort(S.begin(), S.end());
    int ans = 0;
    int l = 0;
    int r = S.size() - 1;
    while (l <= r) {
      int sum = S[l] + S[r];
      if (sum <= M) {
        ans = max(ans, sum);
        l++;
      } else {
        r--;
      }
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

/**
 * File    : JOI_2007_darts.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-3-8 20:45:20
 * Powered by Visual Studio Code
 */

#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <iomanip> // << fixed << setprecision(xxx)
#include <iostream>
#include <map> // if (M.find(key) != M.end()) { }
#include <queue>
#include <random> // random_device rd; mt19937 mt(rd());
#include <set>
#include <stack>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <tuple>
#include <vector>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
int P[1010];
set<int> S;

int main() {
  while (cin >> N >> M && N && M) {
    S.clear();
    for (auto i = 0; i < N; i++) {
      cin >> P[i];
    }
    P[N] = 0;
    N++;
    for (auto i = 0; i < N; i++) {
      for (auto j = i; j < N; j++) {
        S.insert(P[i] + P[j]);
      }
    }
    S.insert(M + 1);
    int ans = 0;
    for (auto i = 0; i < N; i++) {
      for (auto j = i; j < N; j++) {
        int sum = P[i] + P[j];
        if (sum <= M) {
          auto t = S.upper_bound(M - sum);
          t--;
          ans = max(ans, sum + *t);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>

long long kazu[1002001]; // 1001^2
long long n, m, Max = 0, data[1001];

int main() {
  while (std::cin >> n >> m, n * m) {
    if (n == 0 && m == 0)
      break;

    memset(kazu, 0, sizeof(kazu));
    memset(data, 0, sizeof(data));

    for (int i = 0; i < n; i++) {
      std::cin >> data[i + 1];
    }
    for (int c = 0; c < n + 1; c++) {
      for (int d = 0; d < n + 1; d++) {
        kazu[c * n + d] = data[c] + data[d];
      }
    }
    std::sort(kazu, kazu + (n + 1) * (n + 1));

    long long *ptr;
    for (int i = 0; i < (n + 1) * (n + 1); ++i) {
      if (kazu[i] < m) {
        ptr = std::upper_bound(kazu, kazu + (n + 1) * (n + 1), m - kazu[i]) - 1;
        Max = std::max(Max, *ptr + kazu[i]);
      }
    }

    std::cout << Max << std::endl;
    Max = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>

long long kazu[1002001]; // 1001^2
long long n, m, Max, data[1001];

int main() {
  while (true) {
    int s = 0;
    std::cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    data[0] = 0;
    for (int i = 0; i < n; i++) {
      std::cin >> data[i + 1];
    }
    for (int c = 0; c < n + 1; c++) {
      for (int d = 0; d < n + 1; d++) {
        kazu[c * n + d] = data[c] + data[d];
      }
    }
    std::sort(kazu, kazu + (n + 1) * (n + 1));
    for (int a = 0; a < n + 1; a++) {
      for (int b = 0; b < n + 1; b++) {
        while (s < m && Max < m - s) {
          if (std::binary_search(kazu, kazu + ((n + 1) * (n + 1)),
                                 m - s - data[a] - data[b])) {
            Max = std::max(m - s, Max);
            break;
          }
          s++;
        }
        s = 0;
      }
    }
    std::cout << Max << std::endl;
    Max = 0;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n[1000], m[1000], p[10][1000], ans = 0, i = 0;
  vector<int> v;
  while (cin >> n[i] >> m[i]) {
    if (n[i] == 0)
      break;
    for (int j = 0; j < n[i]; j++)
      cin >> p[i][j];
    i++;
  }
  for (int k = 0; k < i; k++) {
    ans = 0;
    v = vector<int>(1, 0);
    for (int a = 0; a < n[k]; a++) {
      v.push_back(p[k][a]);
      for (int b = 0; b < n[k]; b++)
        v.push_back(p[k][a] + p[k][b]);
    }
    sort(v.begin(), v.end());
    for (int j = 0; j < v.size(); j++) {
      if (v[j] > m[k])
        break;
      decltype(v)::iterator itr = upper_bound(v.begin(), v.end(), m[k] - v[j]);
      itr--;
      ans = max(ans, v[j] + *itr);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long m[10], t[10][1000]{0}, a[1000];
  int n[10], i = 0;
  while (1) {
    cin >> n[i] >> m[i];
    if (n[i] == 0)
      break;
    for (int j = 0; j < n[i]; j++)
      cin >> t[i][j];
    i++;
  }
  for (int k = 0; k < i; k++) {
    long long ans = 0;
    for (int j = 0; j < n[k]; j++)
      a[j] = t[k][j];
    sort(a, a + n[k]);
    for (int p = n[k] - 1; p >= 0; p--) {
      if (a[p] > m[k])
        continue;
      else if (a[p] == m[k]) {
        ans = m[k];
        goto ANS;
      } else if (ans > a[p] * 4)
        goto ANS;
      else {
        ans = max(ans, a[p]);
        if (m[k] < a[p] + a[0])
          break;
        for (int q = p; q >= 0; q--) {
          if (a[p] + a[q] > m[k])
            continue;
          else if (a[p] + a[q] == m[k]) {
            ans = m[k];
            goto ANS;
          } else if (ans > a[p] + a[q] * 3)
            break;
          else {
            ans = max(ans, a[p] + a[q]);
            if (m[k] < a[p] + a[q] + a[0])
              break;
            for (int r = q; r >= 0; r--) {
              if (a[p] + a[q] + a[r] > m[k])
                continue;
              else if (a[p] + a[q] + a[r] == m[k]) {
                ans = m[k];
                goto ANS;
              } else if (ans > a[p] + a[q] + a[r] * 2)
                break;
              else {
                ans = max(ans, a[p] + a[q] + a[r]);
                if (m[k] < a[p] + a[q] + a[r] + a[0])
                  break;
                for (int s = r; s >= 0; s--) {
                  if (a[p] + a[q] + a[r] + a[s] > m[k])
                    continue;
                  else if (a[p] + a[q] + a[r] + a[s] == m[k]) {
                    ans = m[k];
                    goto ANS;
                  } else if (ans > a[p] + a[q] + a[r] + a[s])
                    break;
                  else
                    ans = a[p] + a[q] + a[r] + a[s];
                }
              }
            }
          }
        }
      }
    }
  ANS:
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;
int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    n++;
    vector<ll> a(n);
    a[0] = 0;
    FOR(i, 1, n) { cin >> a[i]; }
    int ans = 0;
    vector<int> v;
    REP(i, n) FOR(j, 1, n) {
      if (a[i] + a[j] <= m)
        v.PB(a[i] + a[j]);
    }
    sort(ALL(v));
    v.erase(unique(ALL(v)), v.end());
    REP(i, v.size()) {
      int temp = m - v[i];
      int h, mm, l;
      h = v.size() + 1;
      l = 0;
      mm = (h + l) / 2;
      while (h - l > 1) {
        if (v[mm] > temp)
          h = mm;
        else if (v[mm] < temp)
          l = mm;
        else
          break;
        mm = (h + l) / 2;
      }

      if (v[mm] + v[i] <= m)
        ans = max(ans, v[mm] + v[i]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};

typedef long long int ll;

using namespace std;
int main() {
  while (true) {
    ll n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    n++;
    vector<ll> a(n);
    a[0] = 0;
    FOR(i, 1, n) { cin >> a[i]; }
    ll ans = 0;
    sort(ALL(a));
    FOR(i, 0, n) FOR(j, i, n) FOR(k, j, n) {
      ll p = m - (a[i] + a[j] + a[k]);
      int h, mm, l;
      h = n + 1;
      l = 0;
      mm = (h + l) / 2;
      while (h - l > 1) {
        if (a[mm] > p)
          h = mm;
        else if (a[mm] < p)
          l = mm;
        else
          h = mm;
        mm = (h + l) / 2;
      }
      if (a[i] + a[j] + a[k] + a[mm] <= m)
        ans = max(ans, a[i] + a[j] + a[k] + a[mm]);
    }
    cout << ans << endl;
  }
  return 0;
}