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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int sum = 0;
  vec<int> mi(N), D(N);
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    int d = 0;
    for (auto &c : S) {
      d += (c == '(' ? 1 : -1);
      mi[i] = min(mi[i], d);
    }
    sum += d;
    D[i] = d;
  }
  if (sum) {
    cout << "No\\n";
    return 0;
  }
  vec<int> idx(N);
  iota(idx.begin(), idx.end(), 0);
  sort(idx.begin(), idx.end(), [&](int i, int j) {
    if ((ll)D[i] * D[j] < 0)
      return D[i] > D[j];
    else if (D[i] >= 0 && D[j] >= 0)
      return mi[i] > mi[j];
    else
      return D[i] - mi[i] > D[j] - mi[j];
  });
  int now = 0;
  for (auto &i : idx) {
    if (now + mi[i] < 0) {
      cout << "No\\n";
      return 0;
    }
    now += D[i];
    if (now < 0) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

template <typename Monoid, typename F> class SegmentTree {
private:
  int sz;
  vector<Monoid> seg;
  const F op;
  const Monoid e;

public:
  SegmentTree(int n, const F op, const Monoid &e) : op(op), e(e) {
    sz = 1;
    while (sz <= n)
      sz <<= 1;
    seg.assign(2 * sz, e);
  }
  void set(int k, const Monoid &x) { seg[k + sz] = x; }
  void build() {
    for (int i = sz - 1; i > 0; i--) {
      seg[i] = op(seg[2 * i], seg[2 * i + 1]);
    }
  }
  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = op(seg[2 * k], seg[2 * k + 1]);
    }
  }
  Monoid query(int l, int r) {
    Monoid L = e, R = e;
    for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        L = op(L, seg[l++]);
      if (r & 1)
        R = op(seg[--r], R);
    }
    return op(L, R);
  }
  Monoid operator[](const int &k) const { return seg[k + sz]; }
};

using P = Pa<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int sum = 0;
  int M = 1e6;
  int inf = 1e9;
  auto op = [](P a, P b) {
    if (a.first != b.first)
      return max(a, b);
    else
      return (P){a.first, max(a.second, b.second)};
  };
  vec<priority_queue<int>> list(M + 1);
  SegmentTree<P, decltype(op)> seg(M + 1, op, {-inf, -1});
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    int d = 0;
    int mi = 1e9;
    for (auto &c : S) {
      d += (c == '(' ? 1 : -1);
      mi = min(mi, d);
    }
    sum += d;
    if (mi >= 0)
      list[0].push(d);
    else
      list[-mi].push(d);
  }
  if (sum) {
    cout << "No\\n";
    return 0;
  }
  int now = 0;
  for (int i = 0; i <= M; i++)
    if (!list[i].empty()) {
      seg.update(i, {list[i].top(), i});
      list[i].pop();
    }
  for (int i = 0; i < N; i++) {
    auto [val, id] = seg.query(0, now + 1);
    if (val == -inf) {
      cout << "No\\n";
      return 0;
    }
    now += val;
    if (!list[id].empty()) {
      seg.update(id, {list[id].top(), id});
      list[i].pop();
    } else
      seg.update(id, {-inf, -1});
  }
  cout << "Yes\\n";
}