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

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d", x)
#define plld(x) printf("%lld", x)
#define pds(x) printf("%d ", x)
#define pllds(x) printf("%lld ", x)
#define pdn(x) printf("%d\\n", x)
#define plldn(x) printf("%lld\\n", x)
#define INF 2e9
#define INFLL 4e18
using namespace std;
ll powmod(ll base, ll exponent, ll mod) { // with mod < 1e9
  ll ans = 1;
  while (exponent) {
    if (exponent & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    exponent /= 2;
  }
  return ans;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
const int upperlimit = 2e6 + 100;
const int mod = 1e9 + 7;
pii arr[upperlimit];
bool cmp(pii a, pii b) {
  if (a.F + b.F == 0)
    return (a.S > b.S);
  if ((a.S >= 0) && (b.S >= 0))
    return (a.F > b.F);
  if ((a.S >= 0) && (b.S <= 0))
    return true;
  if ((a.S <= 0) && (b.S >= 0))
    return false;
  return ((a.S - a.F) > (b.S - b.F));
}
int main() {
  //	freopen(".in","r",stdin);freopen(".out","w",stdout);
  int n;
  ll totsum = 0;
  string s;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    int sum = 0, mps = 0;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        sum++;
      else
        sum--;
      mps = min(mps, sum);
    }
    arr[i] = mp(mps, sum);
    totsum += sum;
  }
  if (totsum) {
    cout << "No\\n";
    return 0;
  }
  sort(arr + 1, arr + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    if (totsum + arr[i].F < 0) {
      //			pllds(totsum);pds(i);pdn(arr[i].S);
      cout << "No\\n";
      return 0;
    }
    totsum += arr[i].S;
  }
  cout << "Yes\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d", x)
#define plld(x) printf("%lld", x)
#define pds(x) printf("%d ", x)
#define pllds(x) printf("%lld ", x)
#define pdn(x) printf("%d\\n", x)
#define plldn(x) printf("%lld\\n", x)
#define INF 2e9
#define INFLL 4e18
using namespace std;
ll powmod(ll base, ll exponent, ll mod) { // with mod < 1e9
  ll ans = 1;
  while (exponent) {
    if (exponent & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    exponent /= 2;
  }
  return ans;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
const int upperlimit = 1e6 + 100;
const int mod = 1e9 + 7;
pii arr[upperlimit];
bool cmp(pii a, pii b) {
  if (a.F + b.F == 0)
    return true;
  if ((a.S >= 0) && (b.S >= 0))
    return (a.F > b.F);
  if ((a.S >= 0) && (b.S <= 0))
    return true;
  if ((a.S <= 0) && (b.S >= 0))
    return false;
  return true;
}
int main() {
  //	freopen(".in","r",stdin);freopen(".out","w",stdout);
  int n;
  ll totsum = 0;
  string s;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    int sum = 0, mps = 0;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '(')
        sum++;
      else
        sum--;
      mps = min(mps, sum);
    }
    arr[i] = (mp(mps, sum));
    totsum += sum;
  }
  if (totsum) {
    printf("No\\n");
    return 0;
  }
  sort(arr + 1, arr + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    if (totsum + arr[i].F < 0) {
      //			pllds(totsum);pds(i);pdn(arr[i].S);
      printf("No\\n");
      return 0;
    }
    totsum += arr[i].S;
  }
  printf("Yes\\n");
  return 0;
}