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

/*╔═══╗──╔╗╔╗───────────────────╔╗────────╔═══╦═══╗
  ║║─║╠╗╠╗╔╣╚═╦══╦═╗╔╦╗╔═╗╔══╦══╣╚═╦╦══╦══╣║─║║║─╚╝
  ║╔═╗║╚╝║╚╣║║║╚╝║║─╔╦╗║║║║╚╝║╚╝║╚╝║║║═╬══║╔═╗║╚╩═║
  ╚╝─╚╩══╩═╩╝╚╩══╩╝─╚╩╝╚╝╚╩══╩══╩══╩╩══╩══╩╝─╚╩═══╝*/

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vvll vector<vll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define all(c) c.begin(), c.end()
#define sz(c) (int)c.size()
#define lb lower_bound
#define ub upper_bound
#define ff first
#define ss second
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define nl cout << endl
#define hello cout << "hello" << endl;
#define sp(x) fixed << setprecision(x)
#define re return

void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

const double pi = 3.14159265359;
const ll mod = 1000000007;
const ll modc = 998244353;

ll inline power(ll a, ll b, ll p) {
  a %= p;
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return ans;
}

ll mod_inv(ll n, ll p) { return power(n, p - 2, p); }

bool inline isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}
template <typename... T> void W(T &&...args) {
  ((cout << sp(9) << args << " "), ...);
  nl;
}
template <typename... T> void R(T &&...args) { ((cin >> args), ...); }
ll ston(string s) {
  ll a = 0;
  reverse(s.begin(), s.end());
  ll pow = 1;
  for (int i = 0; i < s.size(); i++) {
    a += pow * (s[i] - '0');
    pow *= 10;
  }
  return a;
}
template <class T> string to_binary(T n) {
  string binaryNum;
  int i = 0;
  while (n > 0) {
    binaryNum.pb((n % 2) + '0');
    n = n / 2;
    i++;
  }
  reverse(binaryNum.begin(), binaryNum.end());
  return binaryNum;
}
template <class T> bool perfectSq(T n) {
  if (sqrt(n) == floor(sqrt(n)))
    return true;
  else
    false;
}
template <class T> void wv(T &v) {
  for (auto x : v)
    cout << x << " ";
  nl;
}
template <class T> void rv(T &v) {
  for (int i = 0; i < sz(v); i++)
    cin >> v[i];
}
bool sortbysec(const pii &a, const pii &b) { return a.ss < b.ss; }
bool sortbyfirstg(const pii &a, const pii &b) { return a.ff > b.ff; }
bool sortbysecg(const pii &a, const pii &b) { return a.ss > b.ss; }
void solve() {
  int i, j;
  ll sum = 0, prod = 1;
  ll maxl = LONG_MIN, minl = LONG_MAX;
  int maxi = INT_MIN, mini = INT_MAX;
  int n;
  R(n);
  vector<string> v(n);
  for (i = 0; i < n; i++)
    cin >> v[i];
  vector<pii> vp;
  int open, close;
  int open_left = 0, right_close = 0;
  map<pii, int> b;
  for (i = 0; i < n; i++) {
    open = close = 0;
    for (j = 0; j < sz(v[i]); j++) {
      if (v[i][j] == '(')
        open++;
      else {
        if (open)
          open--;
        else
          close++;
      }
    }
    if (open && close) {
      vp.pb(mp(open, close));
    } else {
      if (open)
        open_left += open;
      else
        right_close += close;
    }
  }
  vector<pii> A, B;
  map<pii, int> b1;
  for (auto x : vp)
    b1[x]++;
  A = B = vp;
  sort(all(A), sortbyfirstg);
  sort(all(A), sortbysec);
  sort(all(B), sortbysecg);
  sort(all(B));
  int count = 0;
  i = -1;
  j = -1;
  while (count < (int)vp.size()) {
    if (open_left < right_close && i < sz(vp)) {
      i++;
      while (b[A[i]] == b1[A[i]] && i < sz(vp))
        i++;
      if (i == sz(vp)) {
        j++;
        while (b[B[j]] == b1[B[j]] && j < sz(vp))
          j++;
        if (B[j].ff > right_close) {
          cout << "No" << endl;
          re;
        }
        right_close -= B[j].ff;
        right_close += B[j].ss;
        count++;
        b[B[j]]++;
        continue;
      }
      if (A[i].ss > open_left) {
        cout << "No" << endl;
        re;
      }
      open_left -= A[i].ss;
      open_left += A[i].ff;
      b[A[i]]++;
      count++;
    } else if (right_close < open_left && j < sz(vp)) {
      j++;
      while (b[B[j]] == b1[B[j]] && j < sz(vp))
        j++;
      if (j == sz(vp)) {
        i++;
        while (b[A[i]] == b1[A[i]] && i < sz(vp))
          i++;
        if (A[i].ss > open_left) {
          cout << "No" << endl;
          re;
        }
        open_left -= A[i].ss;
        open_left += A[i].ff;
        b[A[i]]++;
        count++;
        continue;
      }
      if (B[j].ff > right_close) {
        cout << "No" << endl;
        re;
      }
      right_close -= B[j].ff;
      right_close += B[j].ss;
      b[B[j]]++;
      count++;
    } else {
      i++;
      j++;
      //	dbg(b[A[i]],b1[A[i]]);
      while (b[A[i]] == b1[A[i]] && i < sz(vp))
        i++;
      while (b[B[j]] == b1[B[j]] && j < sz(vp))
        j++;
      if (A[i].ss < B[j].ff && i < sz(vp)) {
        if (A[i].ss > open_left) {
          cout << "No" << endl;
          re;
        }
        open_left -= A[i].ss;
        open_left += A[i].ff;
        b[A[i]]++;
      } else {
        if (B[j].ff > right_close) {
          cout << "No" << endl;
          re;
        }
        right_close -= B[j].ff;
        right_close += B[j].ss;
        b[B[j]]++;
      }
      count++;
    }
  }
  if (open_left == right_close)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
}


Error: 0

Fixed submission:

/*╔═══╗──╔╗╔╗───────────────────╔╗────────╔═══╦═══╗
  ║║─║╠╗╠╗╔╣╚═╦══╦═╗╔╦╗╔═╗╔══╦══╣╚═╦╦══╦══╣║─║║║─╚╝
  ║╔═╗║╚╝║╚╣║║║╚╝║║─╔╦╗║║║║╚╝║╚╝║╚╝║║║═╬══║╔═╗║╚╩═║
  ╚╝─╚╩══╩═╩╝╚╩══╩╝─╚╩╝╚╝╚╩══╩══╩══╩╩══╩══╩╝─╚╩═══╝*/

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vvll vector<vll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define all(c) c.begin(), c.end()
#define sz(c) (int)c.size()
#define lb lower_bound
#define ub upper_bound
#define ff first
#define ss second
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define nl cout << endl
#define hello cout << "hello" << endl;
#define sp(x) fixed << setprecision(x)
#define re return

void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

const double pi = 3.14159265359;
const ll mod = 1000000007;
const ll modc = 998244353;

ll inline power(ll a, ll b, ll p) {
  a %= p;
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a) % p;
    a = (a * a) % p;
    b >>= 1;
  }
  return ans;
}

ll mod_inv(ll n, ll p) { return power(n, p - 2, p); }

bool inline isPrime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (ll i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}
template <typename... T> void W(T &&...args) {
  ((cout << sp(9) << args << " "), ...);
  nl;
}
template <typename... T> void R(T &&...args) { ((cin >> args), ...); }
ll ston(string s) {
  ll a = 0;
  reverse(s.begin(), s.end());
  ll pow = 1;
  for (int i = 0; i < s.size(); i++) {
    a += pow * (s[i] - '0');
    pow *= 10;
  }
  return a;
}
template <class T> string to_binary(T n) {
  string binaryNum;
  int i = 0;
  while (n > 0) {
    binaryNum.pb((n % 2) + '0');
    n = n / 2;
    i++;
  }
  reverse(binaryNum.begin(), binaryNum.end());
  return binaryNum;
}
template <class T> bool perfectSq(T n) {
  if (sqrt(n) == floor(sqrt(n)))
    return true;
  else
    false;
}
template <class T> void wv(T &v) {
  for (auto x : v)
    cout << x << " ";
  nl;
}
template <class T> void rv(T &v) {
  for (int i = 0; i < sz(v); i++)
    cin >> v[i];
}
bool sortbysec(const pii &a, const pii &b) { return a.ss < b.ss; }
bool sortbyfirstg(const pii &a, const pii &b) { return a.ff > b.ff; }
bool sortbysecg(const pii &a, const pii &b) { return a.ss > b.ss; }
void solve() {
  int i, j;
  ll sum = 0, prod = 1;
  ll maxl = LONG_MIN, minl = LONG_MAX;
  int maxi = INT_MIN, mini = INT_MAX;
  int n;
  R(n);
  vector<string> v(n);
  for (i = 0; i < n; i++)
    cin >> v[i];
  vector<pii> vp;
  int open, close;
  int open_left = 0, right_close = 0;
  map<pii, int> b;
  for (i = 0; i < n; i++) {
    open = close = 0;
    for (j = 0; j < sz(v[i]); j++) {
      if (v[i][j] == '(')
        open++;
      else {
        if (open)
          open--;
        else
          close++;
      }
    }
    if (open && close) {
      vp.pb(mp(open, close));
    } else {
      if (open)
        open_left += open;
      else
        right_close += close;
    }
  }
  vector<pii> A, B;
  map<pii, int> b1;
  for (auto x : vp)
    b1[x]++;
  A = B = vp;
  sort(all(A), sortbyfirstg);
  sort(all(A), sortbysec);
  sort(all(B), sortbysecg);
  sort(all(B));
  int count = 0;
  i = -1;
  j = -1;
  while (count < (int)vp.size()) {
    if (open_left < right_close) {
      i++;
      while (b[A[i]] == b1[A[i]])
        i++;
      if (A[i].ss > open_left) {
        cout << "No" << endl;
        re;
      }
      open_left -= A[i].ss;
      open_left += A[i].ff;
      b[A[i]]++;
      count++;
    } else if (right_close < open_left) {
      j++;
      while (b[B[j]] == b1[B[j]])
        j++;
      if (B[j].ff > right_close) {
        cout << "No" << endl;
        re;
      }
      right_close -= B[j].ff;
      right_close += B[j].ss;
      b[B[j]]++;
      count++;
    } else {
      i++;
      j++;
      //	dbg(b[A[i]],b1[A[i]]);
      while (b[A[i]] == b1[A[i]] && i < sz(vp))
        i++;
      while (b[B[j]] == b1[B[j]] && j < sz(vp))
        j++;
      if (A[i].ss < B[j].ff && i < sz(vp)) {
        if (A[i].ss > open_left) {
          cout << "No" << endl;
          re;
        }
        open_left -= A[i].ss;
        open_left += A[i].ff;
        b[A[i]]++;
      } else {
        if (B[j].ff > right_close) {
          cout << "No" << endl;
          re;
        }
        right_close -= B[j].ff;
        right_close += B[j].ss;
        b[B[j]]++;
      }
      count++;
    }
  }
  if (open_left == right_close)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
}