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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for (int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int n, x, sum;
string arr[1000005];
vector<pi> v;          // num of close, open - close
priority_queue<pi> pq; // num of open, open - close

void fail() {
  cout << "No";
  exit(0);
}

int main() {
  fastio;
  cin >> n;
  FOR(i, 1, n) {
    cin >> arr[i];
    string str = "";
    FOR(j, 0, arr[i].length() - 1) {
      if (arr[i][j] == '(' or str.empty() or str.back() == ')')
        str += arr[i][j];
      else
        str.pop_back();
    }
    arr[i] = str;
    if (arr[i] == "")
      continue;

    int a = 0, b = 0;
    ;
    FOR(j, 0, str.length() - 1) {
      if (str[j] == '(')
        a++;
      else
        b++;
    }
    v.pb(pi(b, a - b));
  }
  sort(all(v), greater<pi>());
  n = v.size();
  FOR(i, 1, n) {
    while (!v.empty() and v.back().f <= sum) {
      pi t = v.back();
      v.pop_back();
      pq.push(pi(t.f + t.s, t.s));
    }
    if (pq.empty())
      fail();
    pi t = pq.top();
    pq.pop();
    if (t.f - t.s > sum)
      fail();
    sum += t.s;
  }
  if (sum != 0)
    fail();
  cout << "Yes";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for (int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int n, sum;
string arr[1000005];
stack<char> s;
vector<pi> v;

void fail() {
  cout << "No";
  exit(0);
}

bool cmp(pi a, pi b) {
  if (a.s == 0)
    return true;
  else if (b.s == 0)
    return false;
  else if (a.f == 0)
    return false;
  else if (b.f == 0)
    return true;
  return a.f - a.s > b.f - b.s;
}

int main() {
  fastio;
  cin >> n;
  FOR(i, 1, n) {
    cin >> arr[i];
    FOR(j, 0, arr[i].length() - 1) {
      if (arr[i][j] == '(')
        s.push('(');
      else {
        if (s.empty() or s.top() == ')')
          s.push(arr[i][j]);
        else
          s.pop();
      }
    }
    string str = "";
    while (!s.empty()) {
      str += s.top();
      s.pop();
    }
    reverse(all(str));
    arr[i] = str;
    if (arr[i] == "")
      continue;

    int a = 0;
    FOR(j, 0, str.length() - 1) a += (str[j] == ')');
    v.pb(pi(str.length() - a, a));
  }
  sort(all(v), cmp);
  return 0;
  for (auto [a, b] : v) {
    if (sum < b)
      fail();
    sum -= b;
    if (sum < 0)
      fail();
    sum += a;
  }
  if (sum != 0)
    fail();
  cout << "Yes";
}