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

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(s, i, n) for (int i = s; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define skip(i, n, p) for (int i = 0; i < n; i += p)
#define all(a) (a).begin(), (a).end()
#define int64 long long
#define debug cerr << __FILE__ << ":" << __LINE__ << endl;
#define INF 2147483647

struct Init {
  Init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.precision(20);
    cout << fixed;
    // setw(6) << setfill('0')
  }
} init;

template <typename _Ty>
ostream &operator<<(ostream &ostr, const vector<_Ty> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    ostr << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return ostr;
}

template <typename _K, typename _V>
ostream &operator<<(ostream &ostr, const map<_K, _V> &v) {
  ostr << "{";
  for (auto iter = v.begin(); iter != v.end(); ++iter) {
    ostr << (*iter).first << ": " << (*iter).second << ", ";
  }
  ostr << "}";
  return ostr;
}

template <typename _K> ostream &operator<<(ostream &ostr, const set<_K> &v) {
  for (auto iter = v.begin(); iter != v.end(); ++iter) {
    ostr << *iter << (iter != v.end() ? " " : "");
  }
  return ostr;
}

template <typename _K, typename _V>
ostream &operator<<(ostream &ostr, const pair<_K, _V> &v) {
  ostr << v.first << " " << v.second;
  return ostr;
}

int64 powmod(int64 n, int64 count, int64 mod) {
  if (count == 0)
    return 1;
  if (count % 2 == 0) {
    int64 t = powmod(n, count / 2, mod);
    return t * t % mod;
  }
  return n * powmod(n, count - 1, mod) % mod;
}

int64 ncr(int64 n, int64 r, int64 mod) {
  int64 a;
  if (n < r)
    a = min(n, r - n);
  else
    a = min(n - r, r);

  int64 res = 1;
  rep(i, a) {
    res = res * (n - i) % mod;
    res = res * powmod(i + 1, mod - 2, mod) % mod;
  }

  return res;
}

int main() {
  int N;
  cin >> N;
  vector<string> L(N);
  rep(i, N) cin >> L[i];
  vector<pair<int, int>> V(N);

  int a = 0;
  int b = 0;
  rep(i, N) {
    V[i] = make_pair(0, 0);
    for (char c : L[i]) {
      if (c == '(') {
        V[i].second++;
      } else {
        V[i].second--;
        V[i].first = min(V[i].first, V[i].second);
      }
    }
  }

  vector<pair<int, int>> aa, bb;
  rep(i, N) {
    if (V[i].second > 0) {
      aa.push_back(V[i]);
    } else {
      bb.push_back(V[i]);
    }
  }

  sort(all(aa));
  reverse(all(aa));

  int tmp = 0;
  rep(i, aa.size()) {
    if (tmp + aa[i].first < 0) {
      cout << "No" << endl;
      exit(0);
    }
    tmp += aa[i].second;
    if (tmp < 0) {
      cout << "No" << endl;
      exit(0);
    }
  }

  sort(all(bb), [](pair<int, int> const &a, pair<int, int> const &b) {
    return a.second - a.first > b.second - b.first;
  });

  rep(i, bb.size()) {
    // cout  << bb[i] << endl;
    if (tmp + bb[i].first < 0) {
      cout << "No" << endl;
      exit(0);
    }
    tmp += bb[i].second;
    if (tmp < 0) {
      cout << "No" << endl;
      exit(0);
    }
  }

  if (tmp == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(s, i, n) for (int i = s; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define skip(i, n, p) for (int i = 0; i < n; i += p)
#define all(a) (a).begin(), (a).end()
#define int64 long long
#define debug cerr << __FILE__ << ":" << __LINE__ << endl;
#define INF 2147483647

struct Init {
  Init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.precision(20);
    cout << fixed;
    // setw(6) << setfill('0')
  }
} init;

template <typename _Ty>
ostream &operator<<(ostream &ostr, const vector<_Ty> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    ostr << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return ostr;
}

template <typename _K, typename _V>
ostream &operator<<(ostream &ostr, const map<_K, _V> &v) {
  ostr << "{";
  for (auto iter = v.begin(); iter != v.end(); ++iter) {
    ostr << (*iter).first << ": " << (*iter).second << ", ";
  }
  ostr << "}";
  return ostr;
}

template <typename _K> ostream &operator<<(ostream &ostr, const set<_K> &v) {
  for (auto iter = v.begin(); iter != v.end(); ++iter) {
    ostr << *iter << (iter != v.end() ? " " : "");
  }
  return ostr;
}

template <typename _K, typename _V>
ostream &operator<<(ostream &ostr, const pair<_K, _V> &v) {
  ostr << v.first << " " << v.second;
  return ostr;
}

int64 powmod(int64 n, int64 count, int64 mod) {
  if (count == 0)
    return 1;
  if (count % 2 == 0) {
    int64 t = powmod(n, count / 2, mod);
    return t * t % mod;
  }
  return n * powmod(n, count - 1, mod) % mod;
}

int64 ncr(int64 n, int64 r, int64 mod) {
  int64 a;
  if (n < r)
    a = min(n, r - n);
  else
    a = min(n - r, r);

  int64 res = 1;
  rep(i, a) {
    res = res * (n - i) % mod;
    res = res * powmod(i + 1, mod - 2, mod) % mod;
  }

  return res;
}

int main() {
  int N;
  cin >> N;
  vector<string> L(N);
  rep(i, N) cin >> L[i];
  vector<pair<int, int>> V(N);

  int a = 0;
  int b = 0;
  rep(i, N) {
    V[i] = make_pair(0, 0);
    rep(j, L[i].size()) {
      if (L[i][j] == '(') {
        V[i].first++;
      } else {
        V[i].second++;
        if (V[i].first > 0) {
          V[i].first--;
          V[i].second--;
        }
      }
    }

    a += V[i].first;
    b += V[i].second;
  }

  if (a != b) {
    cout << "No" << endl;
    exit(0);
  } else {
    // cout << "Yes" << endl;
  }

  sort(all(V), [](pair<int, int> const &a, pair<int, int> const &b) {
    if (a.second == 0)
      return true;
    if (a.first == 0)
      return false;
    if (a.second < b.second && a.second - a.first > 0)
      return true;
    return true;
  });

  int tmp = 0;
  for (auto v : V) {
    // cout << v << endl;
    if (tmp - v.second < 0) {
      cout << "No" << endl;
      exit(0);
    }
    tmp += v.first;
    tmp -= v.second;
  }

  cout << "Yes" << endl;
}