Problem p02686 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

// template {{{  0
// using {{{ 1
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x, y) scanf("%lld%lld", &x, &y)
#define Scll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x, y, z) for (ll x = z; x < y; x++)
#define Rep(x, y) Repe(x, y, 0)
#define RRepe(x, y, z) for (ll x = y - z - 1; x >= 0; x--)
#define RRep(x, y) RRepe(x, y, 0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\\n") : printf("No\\n")
#define YESNO(a) (a) ? printf("YES\\n") : printf("NO\\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

struct brac {
  int val, peek = 0;
};

int main() {

  int N;
  cin >> N;

  vector<string> s(N);

  Rep(i, N) { cin >> s[i]; }

  int X = 0;
  int M = 1e6 + 1;
  vector<vi> a(M);
  vector<vi> b(M);
  vi c;
  ll ab = 0;
  Rep(i, N) {
    int v = 0;
    int pp = 0;
    int np = 0;
    for (char c : s[i]) {
      if (c == '(') {
        v++;
      } else {
        v--;
        np = max(np, -v);
      }
    }
    int K = s[i].size();
    v = 0;
    RRep(j, K) {
      char c = s[i][j];
      if (c == '(') {
        v++;
        pp = max(pp, v);
      } else {
        v--;
      }
    }
    if (v == 0) {
      c.emplace_back(max(np, pp));
    } else if (v > 0) {
      a[np].emplace_back(v);
      X += v;
    } else {
      b[pp].emplace_back(-v);
    }
    ab += v;
  }

  if (ab != 0) {
    YesNo(false);
    return 0;
  }

  int cnt = 0;
  priority_queue<int> q;
  for (int v : a[0])
    q.push(v);
  while (q.size()) {
    int v = q.top();
    q.pop();
    Repe(i, cnt + 1 + v, cnt + 1) {
      for (int v : a[i])
        q.push(v);
    }
    cnt += v;
  }
  if (cnt != X) {
    YesNo(false);
    return 0;
  }

  cnt = 0;
  for (int v : b[0])
    q.push(v);
  while (q.size()) {
    int v = q.top();
    q.pop();
    Repe(i, cnt + 1 + v, cnt + 1) {
      for (int v : b[i])
        q.push(v);
    }
    cnt += v;
  }
  if (cnt != X) {
    YesNo(false);
    return 0;
  }

  bool ans = true;
  for (int v : c) {
    ans = ans && v <= X;
  }

  YesNo(ans);

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

// template {{{  0
// using {{{ 1
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x, y) scanf("%d%d", &x, &y)
#define Scd3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x, y) scanf("%lld%lld", &x, &y)
#define Scll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x, y, z) for (ll x = z; x < y; x++)
#define Rep(x, y) Repe(x, y, 0)
#define RRepe(x, y, z) for (ll x = y - z - 1; x >= 0; x--)
#define RRep(x, y) RRepe(x, y, 0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a) ? printf("Yes\\n") : printf("No\\n")
#define YESNO(a) (a) ? printf("YES\\n") : printf("NO\\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

struct brac {
  int val, peek = 0;
};

int main() {

  int N;
  cin >> N;

  vector<string> s(N);

  Rep(i, N) { cin >> s[i]; }

  int X = 0;
  int M = 1e6 + 1;
  vector<vi> a(M);
  vector<vi> b(M);
  vi c;
  ll ab = 0;
  Rep(i, N) {
    int v = 0;
    int pp = 0;
    int np = 0;
    for (char c : s[i]) {
      if (c == '(') {
        v++;
      } else {
        v--;
        np = max(np, -v);
      }
    }
    int K = s.size();
    v = 0;
    RRep(j, K) {
      char c = s[i][j];
      if (c == '(') {
        v++;
      } else {
        v--;
        np = max(np, -v);
      }
    }
    if (v == 0) {
      c.emplace_back(max(np, pp));
    } else if (v > 0) {
      a[np].emplace_back(v);
      X += v;
    } else {
      b[pp].emplace_back(-v);
    }
    ab += v;
  }

  if (ab != 0) {
    YesNo(false);
    return 0;
  }

  int cnt = 0;
  priority_queue<int> q;
  for (int v : a[0])
    q.push(v);
  while (q.size()) {
    int v = q.top();
    q.pop();
    Repe(i, cnt + 1 + v, cnt + 1) {
      for (int v : a[i])
        q.push(v);
    }
    cnt += v;
  }
  if (cnt != X) {
    YesNo(false);
    return 0;
  }

  cnt = 0;
  for (int v : b[0])
    q.push(v);
  while (q.size()) {
    int v = q.top();
    q.pop();
    Repe(i, cnt + 1 + v, cnt + 1) {
      for (int v : b[i])
        q.push(v);
    }
    cnt += v;
  }
  if (cnt != X) {
    YesNo(false);
    return 0;
  }

  bool ans = true;
  for (int v : c) {
    ans = ans && v <= X;
  }

  YesNo(ans);

  return 0;
}