Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline int read() {
  int x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
inline int rnd(int x) { return rand() % x; }
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 100005;
int n, a[N];
bool check(int x) {
  for (int i = 1; i < n; i++) {
    if (a[n + i] >= x && a[n + i - 1] >= x)
      return 1;
    if (a[n - i] >= x && a[n - i + 1] >= x)
      return 1;
    if (a[n + i] < x && a[n + i - 1] < x)
      return 0;
    if (a[n - i] < x && a[n - i + 1] < x)
      return 0;
  }
  return (a[n] < x) ^ (n & 1);
}
int main() {
  n = read();
  for (int i = 1; i < 2 * n; i++)
    a[i] = read();
  int l = 1, r = n * 2 - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    mid++;
    // cout<<check(mid)<<" "<<l<<" "<<r<<endl;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  writeln(l);
}
/*
1
101
10101
*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline int read() {
  int x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
inline int rnd(int x) { return rand() % x; }
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 100005;
int n, a[N << 1];
bool check(int x) {
  for (int i = 1; i < n; i++) {
    if (a[n + i] >= x && a[n + i - 1] >= x)
      return 1;
    if (a[n - i] >= x && a[n - i + 1] >= x)
      return 1;
    if (a[n + i] < x && a[n + i - 1] < x)
      return 0;
    if (a[n - i] < x && a[n - i + 1] < x)
      return 0;
  }
  return (a[n] < x) ^ (n & 1);
}
int main() {
  n = read();
  for (int i = 1; i < 2 * n; i++)
    a[i] = read();
  int l = 1, r = n * 2 - 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    mid++;
    // cout<<check(mid)<<" "<<l<<" "<<r<<endl;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  writeln(l);
}
/*
1
101
10101
*/