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

#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) {
  return a.first + a.second > b.first + b.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<pair<int, int>> v1, v2;
  int tot = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int cnt = 0, mini = 0;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        cnt++;
      else
        cnt--;
      mini = min(mini, cnt);
    }
    tot += cnt;
    if (cnt >= 0)
      v1.push_back({-mini, cnt});
    else
      v2.push_back({-mini, cnt});
  }
  if (tot != 0) {
    cout << "No";
    return 0;
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end(), cmp);
  int cur = 0;
  for (int i = 0; i < v1.size(); i++) {
    if (v1[i].first > cur) {
      cout << "No";
      return 0;
    }
    cur += v1[i].second;
  }
  // cout<<v1.size()<<endl;
  for (int i = 0; i < v2.size(); i++) {
    if (v2[i].first > cur) {
      cout << "No";
      return 0;
    }
    cur += v2[i].second;
  }
  cout << "Yes";
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[1000005], seg[4000005];
int getmax(int x, int y) {
  if (a[x] > a[y])
    return x;
  return y;
}
void build(int l, int r, int node) {
  if (l == r) {
    seg[node] = l;
    return;
  }
  int m = (l + r) / 2;
  build(l, m, 2 * node);
  build(m + 1, r, 2 * node + 1);
  seg[node] = getmax(seg[2 * node], seg[2 * node + 1]);
}
int query(int l, int r, int a, int b, int node) {
  if (a > r || b < l)
    return 0;
  if (l <= a && b <= r)
    return seg[node];
  int m = (a + b) / 2;
  return getmax(query(l, r, a, m, 2 * node),
                query(l, r, m + 1, b, 2 * node + 1));
}
void update(int x, int v, int a, int b, int node) {
  if (x < a || x > b)
    return;
  if (a == b) {
    seg[node] = v;
    return;
  }
  int m = (a + b) / 2;
  update(x, v, a, m, 2 * node);
  update(x, v, m + 1, b, 2 * node + 1);
  seg[node] = getmax(seg[2 * node], seg[2 * node + 1]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<pair<int, int>> v;
  int tot = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int a = 0, b, cnt = 0;
    for (char j : s) {
      if (j == '(')
        cnt++;
      else
        cnt--;
      a = min(a, cnt);
    }
    b = cnt;
    tot += cnt;
    v.push_back({-a, b});
  }
  if (tot != 0) {
    cout << "No";
    return 0;
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    // cout<<v[i].first<<" "<<v[i].second<<endl;
    a[i + 1] = v[i].second;
  }
  // cout<<"---\\n";
  a[0] = INT_MIN;
  build(0, n, 1);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int l = 0, r = n - 1;
    while (l < r) {
      int m = (l + r + 1) / 2;
      if (v[m].first <= sum)
        l = m;
      else
        r = m - 1;
    }
    // cout<<l<<" ";
    int idx = query(1, l + 1, 0, n, 1);
    if (a[idx] == INT_MIN) {
      cout << i << endl;
      cout << "No";
      return 0;
    }
    sum += a[idx];
    a[idx] = INT_MIN;
    update(idx, INT_MIN, 0, n, 1);
  }
  cout << "Yes";
}