Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_set>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
typedef tuple<int, int, int> t3;

int n, m, a[100010];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n * 2; i++)
    scanf("%d", a + i);
  sort(a + 1, a + 1 + 2 * n);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, (a[i] + a[2 * n + 1 - i]) % m);

  int low = 0, high = n;
  while (low <= high) {
    int mid = (low + high) >> 1;
    int lmx = 0;
    for (int i = 1; i <= mid; i++) {
      lmx = max(lmx, a[i] + a[mid * 2 + 1 - i]);
    }
    if (lmx >= m) {
      high = mid - 1;
      continue;
    }
    int rmn = 2e9;
    for (int i = 2 * n; i > 2 * n - (n - mid); i--) {
      int j = 2 * n + 2 * mid + 1 - i;
      rmn = min(rmn, a[i] + a[j]);
    }
    if (rmn < m) {
      low = mid + 1;
      continue;
    }
    int lv = 0, rv = 0;
    for (int i = 1; i <= mid; i++) {
      lv = max(lv, a[i] + a[mid * 2 + 1 - i]);
    }
    for (int i = 2 * n; i > 2 * n - (n - mid); i--) {
      int j = 2 * n + 2 * mid + 1 - i;
      rv = max(rv, a[i] + a[j] - m);
    }
    if (lv >= rv) {
      ans = min(ans, lv);
      high = mid - 1;
    } else {
      ans = min(ans, rv);
      high = mid - 1;
    }
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_set>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
typedef tuple<int, int, int> t3;

int n, m, a[200010];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n * 2; i++)
    scanf("%d", a + i);
  sort(a + 1, a + 1 + 2 * n);
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, (a[i] + a[2 * n + 1 - i]) % m);

  int low = 0, high = n;
  while (low <= high) {
    int mid = (low + high) >> 1;
    int lmx = 0;
    for (int i = 1; i <= mid; i++) {
      lmx = max(lmx, a[i] + a[mid * 2 + 1 - i]);
    }
    if (lmx >= m) {
      high = mid - 1;
      continue;
    }
    int rmn = 2e9;
    for (int i = 2 * n; i > 2 * n - (n - mid); i--) {
      int j = 2 * n + 2 * mid + 1 - i;
      rmn = min(rmn, a[i] + a[j]);
    }
    if (rmn < m) {
      low = mid + 1;
      continue;
    }
    int lv = 0, rv = 0;
    for (int i = 1; i <= mid; i++) {
      lv = max(lv, a[i] + a[mid * 2 + 1 - i]);
    }
    for (int i = 2 * n; i > 2 * n - (n - mid); i--) {
      int j = 2 * n + 2 * mid + 1 - i;
      rv = max(rv, a[i] + a[j] - m);
    }
    if (lv >= rv) {
      ans = min(ans, lv);
      high = mid - 1;
    } else {
      ans = min(ans, rv);
      high = mid - 1;
    }
  }

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

  return 0;
}