Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
void print(vector<int> x) {
  for (int i = 0; i < (int)x.size(); i++)
    printf("%d%c", x[i], " \n"[i == (int)x.size() - 1]);
}
#define N 100005
int a[N], b[N], n, m;
bool chk(int mid) {
  for (int i = 1; i <= mid; i++)
    b[i] = a[i];
  for (int i = mid + 1; i <= n; i++)
    b[i] = a[i] - m;
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n / 2; i++) {
    if (b[i] + b[n - i + 1] < 0)
      return 0;
  }
  return 1;
}
signed main() {
  cin >> n >> m;
  n *= 2;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  sort(a + 1, a + n + 1);
  int l = 1, r = n + 1, ans;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (chk(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  for (int i = 1; i <= ans; i++)
    b[i] = a[i];
  for (int i = ans + 1; i <= n; i++)
    b[i] = a[i] - m;
  sort(b + 1, b + n + 1);
  ans = 0;
  for (int i = 1; i <= n / 2; i++)
    ans = max(ans, (b[i] + b[n - i + 1]) % m);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
void print(vector<int> x) {
  for (int i = 0; i < (int)x.size(); i++)
    printf("%d%c", x[i], " \n"[i == (int)x.size() - 1]);
}
#define N 200005
int a[N], b[N], n, m;
bool chk(int mid) {
  for (int i = 1; i <= mid; i++)
    b[i] = a[i];
  for (int i = mid + 1; i <= n; i++)
    b[i] = a[i] - m;
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n / 2; i++) {
    if (b[i] + b[n - i + 1] < 0)
      return 0;
  }
  return 1;
}
signed main() {
  cin >> n >> m;
  n *= 2;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  sort(a + 1, a + n + 1);
  int l = 1, r = n + 1, ans;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (chk(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  for (int i = 1; i <= ans; i++)
    b[i] = a[i];
  for (int i = ans + 1; i <= n; i++)
    b[i] = a[i] - m;
  sort(b + 1, b + n + 1);
  ans = 0;
  for (int i = 1; i <= n / 2; i++)
    ans = max(ans, (b[i] + b[n - i + 1]) % m);
  cout << ans << endl;
  return 0;
}