Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 1e5 + 100;
ll a[N];
int32_t main() {
  ll n, m;
  cin >> n >> m;
  for (int i = 1; i <= 2 * n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 2 * n + 1);
  ll l = -1, r = n;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll p1 = 0, ma = 0;
    for (int i = 1; i <= mid; i++) {
      ll z = 2 * mid - i + 1;
      if (a[z] + a[i] >= m)
        p1 = 1;
      ma = max(ma, a[z] + a[i]);
    }
    if (p1) {
      r = mid;
      continue;
    }
    ll t = 2 * mid + 2 * n;
    t /= 2;
    for (int j = 2 * mid + 1; j <= t; j++) {
      ll z = 2 * n - j + 2 * mid + 1;
      if (a[z] + a[j] < m)
        p1 = 1;
      ma = max(ma, (a[z] + a[j]) % m);
    }
    if (!p1)
      r = mid;
    else
      l = mid;
  }
  ll mid = r;
  ll ma = 0;
  for (int i = 1; i <= mid; i++) {
    ll z = 2 * mid - i + 1;
    ma = max(ma, a[z] + a[i]);
  }
  ll t = 2 * mid + 2 * n;
  t /= 2;
  for (int j = 2 * mid + 1; j <= t; j++) {
    ll z = 2 * n - j + 2 * mid + 1;
    ma = max(ma, (a[z] + a[j]) % m);
  }
  cout << ma << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 2e5 + 100;
ll a[N];
int32_t main() {
  ll n, m;
  cin >> n >> m;
  for (int i = 1; i <= 2 * n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 2 * n + 1);
  ll l = -1, r = n;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll p1 = 0, ma = 0;
    for (int i = 1; i <= mid; i++) {
      ll z = 2 * mid - i + 1;
      if (a[z] + a[i] >= m)
        p1 = 1;
      ma = max(ma, a[z] + a[i]);
    }
    if (p1) {
      r = mid;
      continue;
    }
    ll t = 2 * mid + 2 * n;
    t /= 2;
    for (int j = 2 * mid + 1; j <= t; j++) {
      ll z = 2 * n - j + 2 * mid + 1;
      if (a[z] + a[j] < m)
        p1 = 1;
      ma = max(ma, (a[z] + a[j]) % m);
    }
    if (!p1)
      r = mid;
    else
      l = mid;
  }
  ll mid = r;
  ll ma = 0;
  for (int i = 1; i <= mid; i++) {
    ll z = 2 * mid - i + 1;
    ma = max(ma, a[z] + a[i]);
  }
  ll t = 2 * mid + 2 * n;
  t /= 2;
  for (int j = 2 * mid + 1; j <= t; j++) {
    ll z = 2 * n - j + 2 * mid + 1;
    ma = max(ma, (a[z] + a[j]) % m);
  }
  cout << ma << endl;
}