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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double PI = acos(-1);
const double EPS = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<string> s(n);
  vector<P> plu, neg;
  ll sum = 0;
  rep(i, n) {
    cin >> s[i];
    ll cnt = 0, mi = 0;
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        cnt++;
      else
        cnt--;
      chmin(mi, cnt);
    }
    sum += cnt;
    if (cnt >= 0) {
      plu.push_back(P(mi, i));
    } else {
      mi = 0;
      cnt = 0;
      for (int j = s[i].size() - 1; j >= 0; j--) {
        if (s[i][j] == ')')
          cnt++;
        else
          cnt--;
        chmin(mi, cnt);
      }
      neg.push_back(P(mi, i));
    }
  }
  if (sum != 0) {
    cout << "No" << endl;
    return 0;
  }
  string t;
  sort(begin(plu), end(plu), greater<P>());
  sort(begin(neg), end(neg));
  rep(i, plu.size()) t += s[plu[i].se];
  rep(i, neg.size()) t += s[neg[i].se];
  ll cnt = 0;
  rep(i, t.size()) {
    if (t[i] == '(')
      cnt++;
    else
      cnt--;
    if (cnt < 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double PI = acos(-1);
const double EPS = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<string> s(n);
  vector<ll> cnt(n, 0);
  map<ll, ll> mp;
  // mp[0]:() mp[1]:(( mp[2]:)( mp[3]:))
  ll tmp = 0, st = 0, en = 0;
  rep(i, n) {
    cin >> s[i];
    rep(j, s[i].size()) {
      if (s[i][j] == '(')
        cnt[i]++;
      else
        cnt[i]--;
    }
    tmp += cnt[i];
    if (s[i][0] == '(') {
      if (s[i].back() == ')') { //()
        if (cnt[i] == 0)
          mp[0]++;
        if (cnt[i] > 0)
          mp[1]++;
        if (cnt[i] < 0)
          mp[3]++;
      } else { //((
        if (cnt[i] > 0)
          mp[1]++;
        else
          mp[2]++;
      }
    } else {
      if (s[i].back() == ')') { //))
        if (cnt[i] >= 0)
          mp[2]++;
        else
          mp[3]++;
      } else { //)(
        mp[2]++;
      }
    }
  }
  if (tmp != 0) {
    cout << "No" << endl;
    return 0;
  }
  // cout<<mp<<endl;
  if (mp[2] > 0) {
    assert(mp[1] > 0 && mp[3] > 0);
    if (mp[1] > 0 && mp[3] > 0)
      cout << "Yes" << endl;
    else {
      if (mp[1] > 0 && mp[0] > 0) {
        cout << "Yes" << endl;
        return 0;
      }
      if (mp[3] > 0 && mp[0] > 0) {
        cout << "Yes" << endl;
        return 0;
      }
      if (mp[0] >= 2) {
        cout << "Yes" << endl;
        return 0;
      }
      cout << "No" << endl;
    }
  } else {
    cout << "Yes" << endl;
  }
}