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

#include <algorithm>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back()
#define ALL(a) (a).begin(), (a).end()
#define int long long
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;

signed main() {
  int n;
  cin >> n;
  vector<P> v1, v2;
  string s[n];
  rep(i, n) {
    cin >> s[i];
    int res = 0, mini = 0;
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        res++;
      else
        res--;
      chmin(mini, res);
    }
    if (res >= 0) {
      v1.pb({-mini, i});
    } else {
      v2.pb({-(res - mini), i});
    }
  }
  sort(ALL(v1));
  sort(ALL(v2));
  int sz1 = v1.size(), sz2 = v2.size();
  if ((sz1 > 0 && s[v1[0].se][0] == ')') ||
      (sz2 > 0 && s[v2[sz2 - 1].se][s[v2[sz2 - 1].se].size() - 1] == '(')) {
    cout << "No" << endl;
    return 0;
  }
  int now = 0;
  rep(i, sz1) {
    int p = v1[i].se;
    rep(j, s[p].size()) {
      if (s[p][j] == '(')
        now++;
      else
        now--;
      if (now < 0) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  rep(i, sz2) {
    int p = v2[i].se;
    rep(j, s[p].size()) {
      if (s[p][j] == '(')
        now++;
      else
        now--;
      if (now < 0) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << (now == 0 ? "Yes" : "No") << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back()
#define ALL(a) (a).begin(), (a).end()
#define int long long
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;

signed main() {
  int n;
  cin >> n;
  vector<P> v1, v2;
  string s[n];
  rep(i, n) {
    cin >> s[i];
    int res = 0, mini = 0;
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        res++;
      else
        res--;
      chmin(mini, res);
    }
    if (res >= 0) {
      v1.pb({mini, i});
    } else {
      v2.pb({-(res + mini), i});
    }
  }
  sort(ALL(v1));
  sort(ALL(v2));
  if (s[v1[0].se][0] == ')' ||
      s[v2[v2.size() - 1].se][s[v2[v2.size() - 1].se].size() - 1] == '(') {
    cout << "No" << endl;
    return 0;
  }
  int now = 0;
  rep(i, v1.size()) {
    int p = v1[i].se;
    rep(j, s[p].size()) {
      if (s[p][j] == '(')
        now++;
      else
        now--;
      if (now < 0) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  rep(i, v2.size()) {
    int p = v2[i].se;
    rep(j, s[p].size()) {
      if (s[p][j] == '(')
        now++;
      else
        now--;
      if (now < 0) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << (now == 0 ? "Yes" : "No") << endl;
}