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

#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
constexpr ll M = 1e9 + 7;
inline ll sum_1(const ll n) { return ((n * (n + 1)) / 2) % M; }
inline ll sum_2(const ll n) { return ((n * (n + 1) * (2 * n + 1)) / 6) % M; }
inline ll sum_3(const ll n) {
  return (((n * (n + 1)) / 2) % M * ((n * (n + 1)) / 2) % M) % M;
}
inline ll sum_1(const ll i, const ll j) {
  return (sum_1(j) - sum_1(i - 1) + M) % M;
}
inline ll sum_2(const ll i, const ll j) {
  return (sum_2(j) - sum_2(i - 1) + M) % M;
}
inline ll sum_3(const ll i, const ll j) {
  return (sum_3(j) - sum_3(i - 1) + M) % M;
}

ll pow_f(ll b, ll e) {
  ll ans = 1;
  b = b % M;
  while (e) {
    if (e & 1) {
      ans = (ans * b) % M;
    }
    b = (b * b) % M;
    e >>= 1;
  }
  return ans;
}

ll nCr(ll n, ll r) {
  if (r > n || n < 0 || r < 0) {
    return 0;
  }
  if (n == r || r == 0) {
    return 1;
  }
  r = min(r, n - r);
  ll ans = 1;
  for (ll i = 1; i <= r; ++i) {
    ans = (ans * (n - i + 1)) / (i);
  }
  return ans;
}

ll nCr(tuple<ll, ll> t) {
  auto n = get<0>(t);
  auto k = get<1>(t);
  return nCr(n + k - 1, k);
}

template <typename T> void print(const T &v) { cout << v << " "; }

struct temp {
  int first, second, third;
};

ostream &operator<<(ostream &os, temp p) {
  os << p.first << " " << p.second << " " << p.third << "\\n";
  return os;
}

template <typename T> void print(const vector<T> &v) {
  for (auto &i : v) {
    print(i);
  }
  cout << endl;
}

void solve() {
  ll n;
  cin >> n;

  vector<string> s(n);
  for (auto &i : s) {
    cin >> i;
  }

  auto preprocess = [](string &s) -> pair<int, int> {
    int opcnt = 0;
    int clcnt = 0;
    int n = s.size();
    for (int i = 0; i < n; ++i) {
      if (s[i] == '(') {
        ++opcnt;
      } else {
        if (opcnt > 0) {
          --opcnt;
        } else {
          ++clcnt;
        }
      }
    }

    return {opcnt, clcnt};
  };

  vector<temp> pre(n);

  for (int i = 0; i < n; ++i) {
    auto t = preprocess(s[i]);
    pre[i] = {t.first, t.second, i + 1};
  }

  sort(pre.begin(), pre.end(), [](temp &l, temp &r) -> bool {
    if (l.second == 0 || r.second == 0) {
      // OP ONLY
      if (l.second == 0 && r.second != 0) {
        return true;
      } else if (l.second != 0 && r.second == 0) {
        return false;
      } else {
        return l.first > r.first;
      }
    }
    if (l.first == 0 || r.first == 0) {
      // CL ONLY
      if (l.first != 0 && r.first == 0) {
        return true;
      } else if (l.first == 0 && r.first != 0) {
        return false;
      } else {
        return l.second < r.second;
      }
    }
    auto diff1 = l.first - l.second;
    auto diff2 = r.first - r.second;
    if (diff1 >= 0 && diff2 < 0) {
      return true;
    } else if (diff1 < 0 && diff2 >= 0) {
      return false;
    } else if (diff1 >= 0 && diff2 >= 0) {
      if (l.second != r.second) {
        return l.second < r.second;
      } else {
        return l.first > r.first;
      }
    } else {
      if (l.first != r.first) {
        return l.first > r.first;
      } else {
        l.second < r.second;
      }
    }
  });

  int opcnt = 0;
  bool isValid = true;
  for (int i = 0; i < n; ++i) {
    auto t = pre[i];
    if (t.second > opcnt) {
      isValid = false;
      break;
    }
    opcnt -= t.second;
    opcnt += t.first;
  }
  if (isValid && opcnt == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef D_LOCAL
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif

  solve();

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
constexpr ll M = 1e9 + 7;
inline ll sum_1(const ll n) { return ((n * (n + 1)) / 2) % M; }
inline ll sum_2(const ll n) { return ((n * (n + 1) * (2 * n + 1)) / 6) % M; }
inline ll sum_3(const ll n) {
  return (((n * (n + 1)) / 2) % M * ((n * (n + 1)) / 2) % M) % M;
}
inline ll sum_1(const ll i, const ll j) {
  return (sum_1(j) - sum_1(i - 1) + M) % M;
}
inline ll sum_2(const ll i, const ll j) {
  return (sum_2(j) - sum_2(i - 1) + M) % M;
}
inline ll sum_3(const ll i, const ll j) {
  return (sum_3(j) - sum_3(i - 1) + M) % M;
}

ll pow_f(ll b, ll e) {
  ll ans = 1;
  b = b % M;
  while (e) {
    if (e & 1) {
      ans = (ans * b) % M;
    }
    b = (b * b) % M;
    e >>= 1;
  }
  return ans;
}

ll nCr(ll n, ll r) {
  if (r > n || n < 0 || r < 0) {
    return 0;
  }
  if (n == r || r == 0) {
    return 1;
  }
  r = min(r, n - r);
  ll ans = 1;
  for (ll i = 1; i <= r; ++i) {
    ans = (ans * (n - i + 1)) / (i);
  }
  return ans;
}

ll nCr(tuple<ll, ll> t) {
  auto n = get<0>(t);
  auto k = get<1>(t);
  return nCr(n + k - 1, k);
}

template <typename T> void print(const T &v) { cout << v << " "; }

ostream &operator<<(ostream &os, pair<int, int> p) {
  os << p.first << " " << p.second << "\\n";
  return os;
}

template <typename T> void print(const vector<T> &v) {
  for (auto &i : v) {
    print(i);
  }
  cout << endl;
}

void solve() {
  ll n;
  cin >> n;

  vector<string> s(n);
  for (auto &i : s) {
    cin >> i;
  }

  auto preprocess = [](string &s) -> pair<int, int> {
    int opcnt = 0;
    int clcnt = 0;
    int n = s.size();
    for (int i = 0; i < n; ++i) {
      if (s[i] == '(') {
        ++opcnt;
      } else {
        if (opcnt > 0) {
          --opcnt;
        } else {
          ++clcnt;
        }
      }
    }

    return {opcnt, clcnt};
  };

  vector<pair<int, int>> pre(n);

  for (int i = 0; i < n; ++i) {
    auto t = preprocess(s[i]);
    pre[i] = t;
  }

  sort(pre.begin(), pre.end(),
       [](pair<int, int> &l, pair<int, int> &r) -> bool {
         if (l.second == 0 || r.second == 0) {
           return (l.second == 0) ? true : false;
         }
         if (l.first == 0 || r.first == 0) {
           return (l.first == 0) ? false : true;
         }
         if (l.second != r.second) {
           return l.second < r.second;
         } else {
           return l.first > r.first;
         }
       });

  int opcnt = 0;
  bool isValid = true;
  for (int i = 0; i < n; ++i) {
    auto t = pre[i];
    if (t.second > opcnt) {
      isValid = false;
      break;
    }
    opcnt -= t.second;
    opcnt += t.first;
  }

  if (isValid && opcnt == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef D_LOCAL
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif

  solve();

  return 0;
}