Problem p02686 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define hhh cerr << "hhh" << endl
#define see(x) cerr << (#x) << '=' << (x) << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int maxn = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;

string s[maxn];

vector<pr> v1;

struct P {
  int first, second;
  P(int f = 0, int s = 0) : first(f), second(s) {}
  bool operator<(const P &rhs) const {
    return second + first >= rhs.second + rhs.first;
  }
};

vector<P> v2;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int tot = 0;
  int c1 = 0, c2 = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    int tmp = 0, mi = inf;
    for (int j = 0; j < s[i].size(); ++j) {
      tot += (s[i][j] == '(' ? 1 : -1);
      tmp += (s[i][j] == '(' ? 1 : -1);
      mi = min(mi, tmp);
    }
    if (mi >= 0)
      c1 += tmp;
    else {
      if (tmp >= 0)
        v1.push_back(pr(-mi, tmp));
      else
        v2.push_back(P(-mi, tmp));
    }
  }
  if (tot != 0)
    return (cout << "No" << endl), 0;
  sort(v1.begin(), v1.end());
  for (int i = 0; i < v1.size(); ++i) {
    if (c1 >= v1[i].first) {
      c1 += v1[i].second;
    } else
      return (cout << "No" << endl), 0;
  }
  sort(v2.begin(), v2.end());
  for (int i = 0; i < v2.size(); ++i) {
    if (c1 >= v2[i].first) {
      c1 += v2[i].second;
    } else
      return (cout << "No" << endl), 0;
  }
  assert(c1 == 0);
  cout << "Yes" << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define hhh cerr << "hhh" << endl
#define see(x) cerr << (#x) << '=' << (x) << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int maxn = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;

string s[maxn];

vector<pr> v1;

struct P {
  int first, second;
  P(int f = 0, int s = 0) : first(f), second(s) {}
  bool operator<(const P &rhs) const {
    return second + first > rhs.second + rhs.first;
  }
};

vector<P> v2;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int tot = 0;
  int c1 = 0, c2 = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    int tmp = 0, mi = inf;
    for (int j = 0; j < s[i].size(); ++j) {
      tot += (s[i][j] == '(' ? 1 : -1);
      tmp += (s[i][j] == '(' ? 1 : -1);
      mi = min(mi, tmp);
    }
    if (mi >= 0)
      c1 += tmp;
    else {
      if (tmp >= 0)
        v1.push_back(pr(-mi, tmp));
      else
        v2.push_back(P(-mi, tmp));
    }
  }
  if (tot != 0)
    return (cout << "No" << endl), 0;
  sort(v1.begin(), v1.end());
  for (int i = 0; i < v1.size(); ++i) {
    if (c1 >= v1[i].first) {
      c1 += v1[i].second;
    } else
      return (cout << "No" << endl), 0;
  }
  sort(v2.begin(), v2.end());
  for (int i = 0; i < v2.size(); ++i) {
    if (c1 >= v2[i].first) {
      c1 += v2[i].second;
    } else
      return (cout << "No" << endl), 0;
  }
  assert(c1 == 0);
  cout << "Yes" << endl;
}