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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353

using namespace std;
typedef pair<llint, llint> P;

llint n;
string s[1000005];
int lb[1000005], add[1000005];
vector<P> pvec[1000005], nvec[1000005];
bool used[1000005];

bool calc(vector<P> vec[]) {
  priority_queue<P> Q;
  llint h = 0, pre = 0;
  for (int i = 0; i < vec[0].size(); i++)
    Q.push(vec[0][i]);

  while (Q.size() > 0) {
    llint id = Q.top().second;
    Q.pop();
    if (lb[id] > h)
      return false;
    used[id] = true;
    h += add[id];
    if (pre < h) {
      for (int i = pre + 1; i <= h; i++) {
        for (int j = 0; j < vec[i].size(); j++)
          Q.push(vec[i][j]);
      }
      pre = h;
    }
  }
  return true;
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> s[i];

  llint sum = 0;
  for (int i = 1; i <= n; i++) {
    llint h = 0, mn = 0;
    for (int j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '(')
        h++;
      else
        h--;
      mn = min(mn, h);
    }
    sum += h;

    if (h >= 0) {
      lb[i] = -mn, add[i] = h;
      pvec[lb[i]].push_back(P(add[i], i));
    } else {
      mn -= h;
      lb[i] = -mn, add[i] = -h;
      nvec[lb[i]].push_back(P(add[i], i));
    }
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }

  if (!calc(pvec) || !calc(nvec)) {
    cout << "No" << endl;
    return 0;
  }

  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353

using namespace std;
typedef pair<llint, llint> P;

llint n;
string s[1000005];
int lb[1000005], add[1000005];
vector<P> pvec[1000005], nvec[1000005];
bool used[1000005];

bool calc(vector<P> vec[]) {
  priority_queue<P> Q;
  llint h = 0, pre = 0;
  for (int i = 0; i < vec[0].size(); i++)
    Q.push(vec[0][i]);

  while (Q.size() > 0) {
    llint id = Q.top().second;
    Q.pop();
    if (lb[id] > h)
      return false;
    used[id] = true;
    h += add[id];
    if (pre < h) {
      for (int i = pre + 1; i <= h; i++) {
        for (int j = 0; j < vec[i].size(); j++)
          Q.push(vec[i][j]);
      }
      pre = h;
    }
  }
  return true;
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> s[i];

  llint sum = 0;
  for (int i = 1; i <= n; i++) {
    llint h = 0, mn = 0;
    for (int j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '(')
        h++;
      else
        h--;
      mn = min(mn, h);
    }
    sum += h;

    if (h >= 0) {
      lb[i] = -mn, add[i] = h;
      pvec[mn].push_back(P(add[i], i));
    } else {
      mn -= h;
      lb[i] = -mn, add[i] *= h;
      nvec[mn].push_back(P(add[i], i));
    }
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }

  if (!calc(pvec) || !calc(nvec)) {
    cout << "No" << endl;
    return 0;
  }

  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  return 0;
}