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

#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define ld long double
#define showoff                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define f first
#define se second
#define maxn 200005
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define pqueue priority_queue<pii>
#define pdqueue priority_queue<int, vector<int>, greater<int>>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a, int n) {
  a %= mod;
  if (n == 1)
    return a;
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * (power((a * a) % mod, n / 2) % mod)) % mod;
  return power((a * a) % mod, n / 2) % mod;
}

const int inf = (int)1e18;

int inverse(int x) {
  return power(x, mod - 2) % mod; // little fermat....
}

bool check(string x) {
  int tot = 0, fo = 1;
  FOR(i, 0, sz(x)) {
    if (x[i] == '(')
      tot++;
    else
      tot--;
    if (tot < 0)
      return 0;
  }
  if (tot != 0)
    return 0;
  return 1;
}

void solve() {
  int n;
  cin >> n;
  string s[n + 5];
  vector<pii> pos, neg;
  string ans = "";
  FOR(i, 0, n) {
    cin >> s[i];
    int mi = mod, tot = 0;
    FOR(j, 0, sz(s[i])) {
      if (s[i][j] == '(')
        ++tot;
      else
        tot--;
      mi = min(tot, mi);
    }
    if (mi >= 0)
      ans += s[i];
    else if (tot >= 0)
      pos.pb({mi, i});
    else
      neg.pb({mi - tot, i});
  }
  sort(pos.begin(), pos.end(), greater<pii>());
  sort(neg.begin(), neg.end());
  for (auto &x : pos)
    ans += s[x.se];
  for (auto &x : neg)
    ans += s[x.se];
  if (check(ans))
    cout << "Yes";
  else
    cout << "No";
}

signed main() {
  showoff;
  int T = 1;
  // cin >> T;
  FOR(t, 1, T + 1) { solve(); }
  return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define ld long double
#define showoff                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define f first
#define se second
#define maxn 200005
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define pqueue priority_queue<pii>
#define pdqueue priority_queue<int, vector<int>, greater<int>>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// int power(int a,int n)
// {
//     a %= mod;
//     if(n == 1)return a;
//     if(n == 0)return 1;
//     if(n%2)return (a*(power((a*a)%mod,n/2)%mod))%mod;
//     return power((a*a)%mod,n/2)%mod;
// }

// const int inf = (int) 1e18;

// int inverse(int x){
//     return power(x,mod-2)%mod;//little fermat....
// }
bool solve2(string s1, string s2) {
  string t1 = s1 + s2, t2 = s2 + s1;
  int to1 = 0, to2 = 0;
  FOR(i, 0, sz(t1)) {
    if (t1[i] == '(')
      to1++;
    else
      to1--;
    if (t2[i] == '(')
      to2++;
    else
      to2--;
    if (to1 > 0 && to2 < 0)
      return 1;
    if (to2 > 0 && to1 < 0)
      return 0;
  }
  return 1;
}

bool check(string x) {
  int tot = 0, fo = 1;
  FOR(i, 0, sz(x)) {
    if (x[i] == '(')
      tot++;
    else
      tot--;
    if (tot < 0)
      return 0;
  }
  if (tot != 0)
    return 0;
  return 1;
}

void solve() {
  int n;
  cin >> n;
  string s[n + 5];
  FOR(i, 0, n) cin >> s[i];
  sort(s, s + n, solve2);
  string ans = "";
  FOR(i, 0, n) ans += s[i];
  if (check(ans))
    cout << "Yes\\n";
  else
    cout << "No\\n";
}

signed main() {
  showoff;
  int T = 1;
  // cin >> T;
  FOR(t, 1, T + 1) { solve(); }
  return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...