Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define eps 1e-5
#define M 100010
#define fi first
#define se second
#define pb push_back
#define LL long long
#define MOD 1000000007
#define pii pair<int, int>
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline LL llread() {
  char ch = getchar();
  LL x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int a[M], ans, b[M], c[M];
int n, m;
inline bool check(int x) {
  int tot = 0;
  rep(i, x, n) b[++tot] = a[i] - m;
  int i1 = 1, i2 = 1;
  rep(i, 1, n) {
    if (i1 <= x - 1 && i2 <= tot) {
      if (a[i1] < b[i2])
        c[i] = a[i1], ++i1;
      else
        c[i] = b[i2], ++i2;
    } else if (i1 == x) {
      c[i] = b[i2], ++i2;
    } else
      c[i] = a[i1], ++i1;
  }
  ans = 0;
  rep(i, 1, n / 2) {
    int x = c[i] + c[n - i + 1];
    if (x < 0)
      return false;
    ans = max(ans, x);
  }
  return true;
}
int main() {
  // freopen("a.in","r",stdin);
  n = read() * 2, m = read();
  rep(i, 1, n) a[i] = read();
  sort(a + 1, a + n + 1);
  int l = 1, r = n + 1, t = 0;
  // check(6);
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = (t = mid) - 1;
    else
      l = mid + 1;
  }
  check(t);
  // cerr<<t<<endl;
  printf("%d\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define eps 1e-5
#define M 200010
#define fi first
#define se second
#define pb push_back
#define LL long long
#define MOD 1000000007
#define pii pair<int, int>
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline LL llread() {
  char ch = getchar();
  LL x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int a[M], ans, b[M], c[M];
int n, m;
inline bool check(int x) {
  int tot = 0;
  rep(i, x, n) b[++tot] = a[i] - m;
  int i1 = 1, i2 = 1;
  rep(i, 1, n) {
    if (i1 <= x - 1 && i2 <= tot) {
      if (a[i1] < b[i2])
        c[i] = a[i1], ++i1;
      else
        c[i] = b[i2], ++i2;
    } else if (i1 == x) {
      c[i] = b[i2], ++i2;
    } else
      c[i] = a[i1], ++i1;
  }
  ans = 0;
  rep(i, 1, n / 2) {
    int x = c[i] + c[n - i + 1];
    if (x < 0)
      return false;
    ans = max(ans, x);
  }
  return true;
}
int main() {
  // freopen("a.in","r",stdin);
  n = read() * 2, m = read();
  rep(i, 1, n) a[i] = read();
  sort(a + 1, a + n + 1);
  int l = 1, r = n + 1, t = 0;
  // check(6);
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = (t = mid) - 1;
    else
      l = mid + 1;
  }
  check(t);
  // cerr<<t<<endl;
  printf("%d\n", ans);
}