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

#pragma GCC optimize("O3") //
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, b, a) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
typedef unsigned long long ull;

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 eng(seed);

uniform_int_distribution<> DD(0, (1ll << 31));

ll random2() {
  return (ll(DD(eng)) * (1ll << 31) + ll(DD(eng))) & ((1ll << 62ll) - 1);
}

ll n, m, k, q, T;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod = 998244353;

const int MAXN = 1000001;

vector<string> S;
vi ind;
int dip[MAXN] = {0};
int rise[MAXN] = {0};

bool comp(ll i, ll j) {
  if (rise[i] >= 0 && rise[j] >= 0) {
    return dip[i] > dip[j];
  }
  if (rise[i] < 0 && rise[j] < 0) {
    return dip[i] - rise[i] < dip[j] - rise[j];
  }
  return rise[i] > rise[j];
}

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

  // freopen("input.txt","r",stdin);
  // freopen("autput.txt","w",stdout);

  ll a, b, c, d, e;

  cin >> n;
  rep(c1, 0, n) {
    string s;
    cin >> s;
    S.push_back(s);
    ind.push_back(c1);
    d = 0;
    a = 0;
    trav(ch, s) {
      if (ch == '(') {
        a++;
      } else {
        a--;
      }
      d = min(a, d);
    }
    dip[c1] = d;
    rise[c1] = a;
  }

  sort(all(ind), comp);
  a = 0;
  d = 0;
  trav(i, ind) {
    trav(ch, S[i]) {
      if (ch == '(') {
        a++;
      } else {
        a--;
      }
      d = min(a, d);
    }
  }
  if (a != 0 || d < 0) {
    cout << "No\\n";
  } else {
    cout << "Yes\\n";
  }

  return 0;
}


Error: WA

Fixed submission:

#pragma GCC optimize("O3") //
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, b, a) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
typedef unsigned long long ull;

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 eng(seed);

uniform_int_distribution<> DD(0, (1ll << 31));

ll random2() {
  return (ll(DD(eng)) * (1ll << 31) + ll(DD(eng))) & ((1ll << 62ll) - 1);
}

ll n, m, k, q, T;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod = 998244353;

const int MAXN = 300001;

vector<string> S;
vi ind;
int dip[MAXN] = {0};
int rise[MAXN] = {0};

bool comp(ll i, ll j) {
  if (rise[i] >= 0 && rise[j] >= 0) {
    return dip[i] > dip[j];
  }
  if (rise[i] < 0 && rise[j] < 0) {
    return dip[i] - rise[i] < dip[j] - rise[j];
  }
  return rise[i] > rise[j];
}

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

  // freopen("input.txt","r",stdin);
  // freopen("autput.txt","w",stdout);

  ll a, b, c, d, e;

  cin >> n;
  rep(c1, 0, n) {
    string s;
    cin >> s;
    S.push_back(s);
    ind.push_back(c1);
    d = 0;
    a = 0;
    trav(ch, s) {
      if (ch == '(') {
        a++;
      } else {
        a--;
      }
      d = min(a, d);
    }
    dip[c1] = d;
    rise[c1] = a;
  }

  sort(all(ind));
  a = 0;
  d = 0;
  trav(i, ind) {
    trav(ch, S[i]) {
      if (ch == '(') {
        a++;
      } else {
        a--;
      }
      d = min(a, d);
    }
  }
  if (a != 0 || d < 0) {
    cout << "No\\n";
  } else {
    cout << "Yes\\n";
  }

  return 0;
}