alexjercan/bugnet huggingface dataset:
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, p[1001];
vector<int> v;
int nibuntansaku(int x) {
  int l = 0, r = v.size();
  while (l < r) {
    int mid = (l + r) / 2;
    if (v[mid] > x)
      r = mid;
    else
      l = mid + 1;
  }
  if (v[l] > x && l != 0)
    l--;
  return v[l];
}
main() {
  while (cin >> n >> m, n) {
    v.clear();
    v.push_back(0);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
      v.push_back(p[i]);
    }
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++)
        v.push_back(p[i] + p[j]);
    sort(v.begin(), v.end());
    int sum = 0;
    for (int i = 0; i < v.size(); i++) {
      int x = nibuntansaku(m - v[i]) + v[i];
      if (x <= m)
        sum = max(x, sum);
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, p[101];
vector<int> v;
int nibuntansaku(int x) {
  int l = 0, r = v.size();
  while (l < r) {
    int mid = (l + r) / 2;
    if (v[mid] > x)
      r = mid;
    else
      l = mid + 1;
  }
  if (v[l] > x && l != 0)
    l--;
  return v[l];
}
int main() {
  while (cin >> n >> m, n) {
    v.clear();
    v.push_back(0);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
      v.push_back(p[i]);
    }
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++)
        v.push_back(p[i] + p[j]);
    sort(v.begin(), v.end());
    int sum = 0;
    for (int i = 0; i < v.size(); i++) {
      int x = nibuntansaku(m - v[i]) + v[i];
      if (x <= m)
        sum = max(x, sum);
    }
    cout << sum << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int tsP[1001 * 1001];
int main() {
  int N, M;
  int P[1001];
  int ans;
  int cnt;

  P[0] = 0;

  // cin >> N >> M;
  while (cin >> N >> M, N && M) {
    ans = 0;
    cnt = 0;
    for (int i = 1; i <= N; i++) {
      cin >> P[i];
    }

    for (int i = 0; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        if (P[i] + P[j] <= M) {
          tsP[cnt++] = P[i] + P[j];
        }
      }
    }
    sort(tsP, tsP + cnt);
    for (int i = 0; i < cnt; i++) {
      // for (int j = i; j < cnt; j++) {
      // if (tsP[i] + tsP[j] <= M) {
      ans = max(ans, /*tsP[i] + tsP[j]*/ tsP[i] +
                         *(upper_bound(tsP, tsP + cnt, M - tsP[i]) - 1));
      //}
      //}
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int tsP[1001 * 1001];
int main() {
  int N, M;
  int P[1001];
  int ans;
  int cnt;

  P[0] = 0;

  // cin >> N >> M;
  while (cin >> N >> M, N && M) {
    ans = 0;
    cnt = 0;
    for (int i = 1; i <= N; i++) {
      cin >> P[i];
    }

    for (int i = 0; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        if (P[i] + P[j] <= M) {
          tsP[cnt++] = P[i] + P[j];
        }
      }
    }
    sort(tsP, tsP + cnt);
    for (int i = 0; i < cnt; i++) {
      for (int j = i; j < cnt; j++) {
        if (tsP[i] + tsP[j] <= M) {
          ans = max(ans, tsP[i] + tsP[j]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m;
vector<int> p;
int t;
vector<int> q;
vector<int> o;
int ans;

int b(int x) {
  int l = 0;
  int r = q.size();
  while (r - l > 1) {
    int i = (l + r) / 2;
    //		printf("l=%d, r=%d, i=%d, q[i]=%d, x=%d\\n", l, r, i, q[i], x);
    if (q[i] <= x) {
      l = i;
    } else {
      r = i;
    }
  }
  //	printf("%d\\n", q[l]);
  return q[l];
}

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    p = o;
    p.push_back(0);
    for (int i = 0; i < n; i++) {
      scanf("%d", &t);
      p.push_back(t);
    }
    q = o;
    ans = 0;

    for (int i = 0; i < p.size(); i++) {
      for (int j = 0; j < p.size(); j++) {
        q.push_back(p[i] + p[j]);
      }
    }
    sort(q.begin(), q.end());
    /*
                    for(int i=0; i<q.size(); i++)
                            printf("%d\\n", q[i]);
    */
    for (int i = 0; i < q.size(); i++) {
      int a = q[i];
      if (m - a < 0)
        continue;
      ans = max(ans, b(m - a) + a);
      //			printf("%d\\n", b(m - a));
    }

    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

long long int n, m;
long long int p[1000];
long long int s;
vector<long long int> a;
vector<long long int> b;

void f(long long int x) {
  if (x <= m)
    s = max(s, x);
}

int main() {
  while (1) {
    scanf("%lld %lld", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(p, 0, sizeof(p));
    for (int i = 0; i < n; i++)
      scanf("%lld", &p[i]);
    a = b;
    s = 0;

    for (int i = 0; i < n; i++) {
      f(p[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= i; j++) {
        if (p[i] + p[j] <= m) {
          a.push_back(p[i] + p[j]);
        }
        f(p[i] + p[j]);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < a.size(); j++) {
        f(p[i] + a[j]);
      }
    }
    for (int i = 0; i < a.size(); i++) {
      for (int j = 0; j < a.size(); j++) {
        f(a[i] + a[j]);
      }
    }
    printf("%lld\\n", s);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n, m;
  while (cin >> n >> m, n) {
    int p[n];
    for (int i = 0; i < n; i++)
      cin >> p[i];
    vector<int> s;
    s.emplace_back(0);
    s.emplace_back(m + 1);
    for (int i = 0; i < n; i++)
      s.emplace_back(p[i]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        s.emplace_back(p[i] + p[j]);
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    int ans = 0;
    auto latte = --s.end();
    for (int x : s) {
      if (x > m)
        continue;
      while (latte != s.begin() && x + *latte > m)
        --latte;
      int y = *latte;
      if (x + y > m)
        continue;
      ans = max(ans, x + y);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int n, m;
  while (cin >> n >> m, n) {
    int p[n];
    for (int i = 0; i < n; i++)
      cin >> p[i];
    set<int> s;
    s.insert(0);
    s.insert(m + 1);
    for (int i = 0; i < n; i++)
      s.insert(p[i]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        s.insert(p[i] + p[j]);
    int ans = 0;
    auto latte = --s.end();
    for (int x : s) {
      if (x > m)
        continue;
      while (latte != s.begin() && x + *latte > m)
        --latte;
      int y = *latte;
      if (x + y > m)
        continue;
      ans = max(ans, x + y);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
static const int MAX_N = 1000;
int main() {
  int N, M;
  int point[MAX_N + 1];
  int a[(MAX_N + 1) * (MAX_N + 1)];
  for (;;) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;
    point[0] = 0;
    for (int i = 1; i <= N; i++)
      scanf("%d", &point[i]);
    int index = 0;
    for (int i = 0; i < N + 1; i++) {
      for (int j = 0; j < N + 1; j++) {
        a[index] = point[i] + point[j];
        index++;
      }
    }
    sort(a, a + (N + 1) * (N + 1));
    int num_a = (N + 1) * (N + 1);
    int ans = 0;
    for (int i = 0; i < num_a; i++) {
      if (a[i] > M)
        break;
      int lb = 0, ub = num_a - 1;
      int mid;
      while (ub - lb > 1) {
        mid = (lb + ub) / 2;
        if (a[i] + a[mid] > M)
          ub = mid;
        else
          lb = mid;
      }
      mid = (lb + ub) / 2;
      if (a[i] + a[mid] > ans) {
        ans = a[i] + a[mid];
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
static const int MAX_N = 100;
int main() {
  int N, M;
  int point[MAX_N + 1];
  int a[(MAX_N + 1) * (MAX_N + 1)];
  for (;;) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;
    point[0] = 0;
    for (int i = 1; i <= N; i++)
      scanf("%d", &point[i]);
    int index = 0;
    for (int i = 0; i < N + 1; i++) {
      for (int j = 0; j < N + 1; j++) {
        a[index] = point[i] + point[j];
        index++;
      }
    }
    sort(a, a + (N + 1) * (N + 1));
    int num_a = (N + 1) * (N + 1);
    int ans = 0;
    for (int i = 0; i < num_a; i++) {
      if (a[i] > M)
        break;
      int lb = 0, ub = num_a - 1;
      int mid;
      while (ub - lb > 1) {
        mid = (lb + ub) / 2;
        if (a[i] + a[mid] > M)
          ub = mid;
        else
          lb = mid;
      }
      mid = (lb + ub) / 2;
      if (a[i] + a[mid] > ans) {
        ans = a[i] + a[mid];
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> mato;
vector<int> a;

int main() {
  int n, m;
  int self;
  while (scanf("%d %d", &n, &m) && n && m) {
    mato.clear();
    a.clear();

    for (int i = 0; i < n; i++) {
      scanf("%d", &self);
      mato.push_back(self);
    }
    mato.push_back(0);
    sort(mato.begin(), mato.end());
    mato.erase(unique(mato.begin(), mato.end()), mato.end());
    int size = mato.size();
    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size; j++) {
        a.push_back(mato[i] + mato[j]);
      }
    }
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    int max = 0;
    size = a.size();
    int s, ss;
    for (int i = 0; i < size; i++) {
      s = m - a[i];
      if (s > 0) {
        ss = *(lower_bound(a.begin(), a.end(), s) - 1);
        // printf("find:%d -> %d\\n",s,ss);
        if (a[i] + ss > max) {
          max = a[i] + ss;
        }
      }
    }
    printf("%d\\n", max);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> mato;
vector<int> a;

int main() {
  int n, m;
  int self;
  while (scanf("%d %d", &n, &m) && n && m) {
    mato.clear();
    a.clear();

    for (int i = 0; i < n; i++) {
      scanf("%d", &self);
      mato.push_back(self);
    }
    mato.push_back(0);
    mato.erase(unique(mato.begin(), mato.end()), mato.end());
    int s = mato.size();
    for (int i = 0; i < s; i++) {
      for (int j = 0; j < s; j++) {
        a.push_back(mato[i] + mato[j]);
      }
    }
    a.erase(unique(a.begin(), a.end()), a.end());
    int max = 0;
    s = a.size();
    for (int i = 0; i < s; i++) {
      for (int j = 0; j < s; j++) {
        if (a[i] + a[j] < m && a[i] + a[j] > max) {
          max = a[i] + a[j];
        }
      }
    }
    printf("%d\\n", max);
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, M;
int NN;
int *dd;
int *d;

int search(int var) {
  int i = 0, j = NN - 1;
  int cut;

  while (i != j) {
    cut = (i + j) / 2;
    if (dd[cut] + var > M) {
      j = cut;
    } else if (dd[cut] + var < M) {
      i = cut + 1;
    } else
      return dd[cut] + var;
  }

  if (var + dd[cut] <= M)
    return dd[cut] + var;

  return -1;
}
int main(void) {

  while (cin >> N >> M, N && M) {

    NN = (N + 1) * (N + 1);
    d = new int[N + 1];
    dd = new int[NN];

    d[0] = 0;

    for (int i = 1; i <= N; i++)
      cin >> d[i];

    int c = 0;
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= N; j++, c++)
        dd[c] = d[i] + d[j];

    sort(dd, dd + NN);

    c = 0;
    int num = 0;
    int MAX = 0;

    for (int i = 0; i < NN; i++) {
      if (dd[i] > M)
        break;

      num = search(dd[i]);

      if (num > MAX)
        MAX = num;
    }
    cout << MAX << endl;

    delete[] d;
    delete[] dd;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int N, M;
int NN;
int *dd;
int *d;

int search(int var) {
  int i = 0, j = NN - 1;
  int cut;

  while (i != j) {
    cut = (i + j) / 2;
    if (dd[cut] + var > M) {
      j = cut - 1;
    } else if (dd[cut] + var < M) {
      i = cut + 1;
    } else
      return dd[cut] + var;
  }

  if (var + dd[cut] <= M)
    return dd[cut] + var;

  return -1;
}
int main(void) {

  while (cin >> N >> M, N && M) {

    NN = (N + 1) * (N + 1);
    d = new int[N + 1];
    dd = new int[NN];

    d[0] = 0;

    for (int i = 1; i <= N; i++)
      cin >> d[i];

    int c = 0;
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= N; j++, c++)
        dd[c] = d[i] + d[j];

    sort(dd, dd + NN);

    c = 0;
    int num = 0;
    int MAX = 0;

    for (int i = 0; i < NN; i++) {
      if (dd[i] > M)
        break;

      num = search(dd[i]);

      if (num > MAX)
        MAX = num;
    }
    cout << MAX << endl;

    delete[] d;
    delete[] dd;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

void func(int N, int M) {
  vector<int> First, Second;

  First.push_back(0);
  int tmp, max;

  for (int i = 0; i < N; i++) {
    scanf("%d", &tmp);
    First.push_back(tmp);
  }

  sort(First.begin(), First.end());

  for (int i = 0; i <= N; i++) {
    for (int k = i; k <= N; k++) {
      if (First[i] + First[k] <= M) {
        Second.push_back(First[i] + First[k]);
      } else {
        break;
      }
    }
  }

  sort(Second.begin(), Second.end());

  int limit = Second.size() - 1, div, left, right, max_min, m;

  max = 0;
  for (int i = limit; i >= 0; i--) {
    div = M - Second[i];
    max_min = 0;
    left = 0, right = limit;
    m = (left + right) / 2;
    while (left <= right) {
      if (Second[m] > div) {
        right = m - 1;
      } else {
        max_min = (max_min >= Second[m]) ? max_min : Second[m];
        left = m + 1;
      }
      m = (left + right) / 2;
    }
    max = (max >= Second[i] + max_min) ? max : Second[i] + max_min;
  }

  printf("%d\\n", max);
}

int main() {

  int N, M;

  while (true) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;

    func(N, M);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

void func(int N, int M) {
  vector<int> First, Second;

  First.push_back(0);
  int tmp, max = 0;

  for (int i = 0; i < N; i++) {
    scanf("%d", &tmp);
    if (tmp <= M) {
      First.push_back(tmp);
    }
  }

  Second.push_back(0);
  for (int i = 0; i < First.size(); i++) {
    for (int k = i; k < First.size(); k++) {
      if (First[i] + First[k] <= M) {
        Second.push_back(First[i] + First[k]);
      }
    }
  }

  for (int i = 0; i < Second.size(); i++) {
    for (int k = i; k < Second.size(); k++) {
      if (Second[i] + Second[k] <= M && Second[i] + Second[k] > max) {
        max = Second[i] + Second[k];
      }
    }
  }

  printf("%d\\n", max);
}

int main() {

  int N, M;

  while (true) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;

    func(N, M);
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n, m;
int p[1001];
int pp[1500000];
int main() {
  while (scanf("%d%d", &n, &m)) {
    if (!n && !m)
      break;
    memset(p, 0, sizeof(p));
    for (int i = 0; i < n; i++)
      scanf("%d", &p[i]);
    sort(p, p + (++n));
    for (int c = 0; c <= n; c++) {
      for (int d = 0; d <= n; d++) {
        pp[c * n + d] = p[c] + p[d];
      }
    }
    sort(pp, pp + n * n);
    int res = 0;
    for (int i = 0; i < n * n; i++) {
      int temp = upper_bound(pp, pp + n * n, m - pp[i]) - pp - 1;
      if (pp[temp] + pp[i] <= m)
        res = max(res, pp[temp] + pp[i]);
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n, m;
int p[1001];
int pp[1500000];
bool binary_search(int x) {
  int l = 0, r = (n + 1) * (n + 1);
  while (r - l >= 1) {
    int i = (l + r) / 2;
    if (pp[i] == x)
      return true;
    else if (pp[i] < x)
      l = i + 1;
    else
      r = i;
  }
  return false;
}
bool C(int x) {
  for (int a = 0; a <= n; a++) {
    for (int b = 0; b <= n; b++) {
      if (binary_search(x - p[a] - p[b])) {
        return true;
      }
    }
  }
  return false;
}
int main() {
  while (scanf("%d%d", &n, &m)) {
    if (!n && !m)
      break;
    memset(p, 0, sizeof(p));
    for (int i = 0; i < n; i++)
      scanf("%d", &p[i]);
    sort(p, p + n + 1);
    for (int c = 0; c <= n; c++) {
      for (int d = 0; d <= n; d++) {
        pp[c * (n + 1) + d] = p[c] + p[d];
      }
    }
    sort(pp, pp + (n + 1) * (n + 1));
    for (int i = m; i >= 0; i--) {
      if (C(i)) {
        printf("%d\\n", i);
        break;
      }
    }
  }
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const LL LINF = 1LL << 60;
const LL MOD = 100000;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define all(c) (c).begin(), (c).end()

struct edge {
  LL to, cost;
  edge(int t, LL c) : to(t), cost(c) {}
};

int dxk[] = {1, 1, 1, 0, -1, 0};
int dxg[] = {0, 1, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, 0, -1};

int main() {
  int n;
  LL m;
  while (cin >> n >> m && n) {
    vector<LL> nums(n);
    rep(i, n) cin >> nums[i];
    vector<LL> arr(n * n + n);
    rep(i, n) rep(j, n) { arr[i * n + j] = nums[i] + nums[j]; }
    rep(i, n) arr[n * n + i] = nums[i];
    sort(all(arr));

    LL ans = 0;
    for (auto x : arr) {
      LL rem = m - x;
      auto it = upper_bound(all(arr), rem);
      if (it != arr.begin()) {
        LL num = *(--it);
        ans = max(ans, num + x);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const LL LINF = 1LL << 60;
const LL MOD = 100000;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define all(c) (c).begin(), (c).end()

struct edge {
  LL to, cost;
  edge(int t, LL c) : to(t), cost(c) {}
};

int dxk[] = {1, 1, 1, 0, -1, 0};
int dxg[] = {0, 1, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, 0, -1};

int main() {
  int n;
  LL m;
  vector<LL> anses;
  while (cin >> n >> m && n) {
    vector<LL> nums(n);
    rep(i, n) cin >> nums[i];
    vector<LL> arr(n * n + 2);
    rep(i, n) rep(j, n) { arr[i * n + j] = nums[i] + nums[j]; }
    rep(i, n) arr[n * n + i] = nums[i];
    sort(all(arr));

    LL ans = 0;
    for (auto x : arr) {
      LL rem = m - x;
      auto it = upper_bound(all(arr), rem);
      if (it != arr.begin()) {
        LL num = *(--it);
        ans = max(ans, num + x);
      }
    }
    cout << ans << endl;
    anses.push_back(ans);
  }
  cout << "anses" << endl;
  for (auto x : anses)
    cout << x << endl;
  cin >> n;
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX_N (1000)
#define MAX_M (2 * 1e8)

using namespace std;

int field[MAX_N + 1];
int fld[(MAX_N + 1) * (MAX_N + 1)];

int N, M;

int ans;

int bin_search(int tar, int *s, int *e) {
  int result;
  int width = e - s;
  while (true) {
    width = e - s;
    if (width <= 1) {
      result = *s;
      break;
    }

    if (*(s + (width / 2)) <= tar) {
      s = s + (width / 2);
    } else {
      e = s + (width / 2);
    }
  }

  return result;
}

void solve() {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      fld[(N + 1) * i + j] = field[i] + field[j];
    }
  }

  sort(fld, fld + (N + 1) * (N + 1));

  ans = 0;
  int rest;
  int sample;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      rest = M - (field[i] + field[j]);
      if (rest < 0)
        break;
      sample = bin_search(rest, fld, fld + (N + 1) * (N + 1));
      ans = max(ans, field[i] + field[j] + sample);
    }
  }

  return;
}

int main() {

  while (true) {
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0)
      break;
    for (int i = 1; i <= N; i++) { // fiest element is 0;
      scanf("%d", field + i);
    }

    solve();

    printf("%d\\n", ans);
  }

  return 0;
}

Error: WA

Fixed submission:

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

#define MAX_N (1000)
#define MAX_M (2 * 1e8)

using namespace std;

int field[MAX_N];
int fld[MAX_N * MAX_N];

int N, M;

int ans;

int bin_search(int tar, int *s, int *e) {
  int result;
  int width = e - s;
  while (true) {
    width = e - s;
    if (width <= 1) {
      result = *s;
      break;
    }

    if (*(s + (width / 2)) <= tar) {
      s = s + (width / 2);
    } else {
      e = s + (width / 2) - 1;
    }
  }

  return result;
}

void solve() {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      fld[(N + 1) * i + j] = field[i] + field[j];
    }
  }

  sort(fld, fld + (N + 1) * (N + 1));

  int rest;
  int sample;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      rest = M - (field[i] + field[j]);
      if (rest < 0)
        break;
      sample = bin_search(rest, fld, fld + (N + 1) * (N + 1));
      ans = max(ans, field[i] + field[j] + sample);
    }
  }

  return;
}

int main() {

  while (true) {
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0)
      break;
    for (int i = 1; i <= N; i++) { // fiest element is 0;
      scanf("%d", field + i);
    }

    solve();

    printf("%d\\n", ans);
  }

  return 0;
}