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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define RFOR(i, a, b) for (int i = b - 1; i >= a; --i)

#define all(x) begin(x), end(x)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os << "]";
}

// edit

pair<int, int> kakko(string s) {
  vector<char> st;

  for (auto e : s) {
    if (st.empty()) {
      st.push_back(e);
    } else if (st.back() == '(') {
      if (e == ')') {
        st.pop_back();
      } else {
        st.push_back(e);
      }
    } else {
      st.push_back(e);
    }
  }

  int left = 0, right = 0;
  for (auto e : st) {
    if (e == ')')
      left++;
    else
      right++;
  }

  return make_pair(left, right);
}

void solve() {
  int N;
  cin >> N;

  vector<string> S(N);
  REP(i, N) cin >> S[i];

  vector<pair<int, int>> tapu_list;

  for (auto e : S) {
    tapu_list.push_back(kakko(e));
    if (tapu_list.back() == pair<int, int>(0, 0))
      tapu_list.pop_back();
  }
  if (tapu_list.size() == 0) {
    cout << "Yes" << endl;
    return;
  }

  ll sum = 0;
  vector<int> starts, goals;
  //    set<int> st;

  //    sort(all(tapu_list), [](auto l, auto r){
  //        if(l.first == 0 && r.first > 0) return true;
  //        if(l.second == 0 && r.second > 0) return false;
  //        if(r.first == 0 && l.first > 0) return false;
  //        if(r.second == 0 && l.second > 0) return true;
  //        return (l.second - l.first) > (r.second - l.first);
  //    });

  vector<pair<int, int>> lefts, rights, mids;

  for (int i = 0; i < tapu_list.size(); ++i) {
    auto e = tapu_list[i];
    sum += e.first;
    sum -= e.second;

    //        if(e.first == 0) start = true;
    //        if(e.second == 0) goal = true;

    //        if(e.first == 0 || e.second == 0){
    //            st.insert(i);
    //        }

    //        if(e.first == 0) starts.push_back(i);
    //        if(e.second == 0) goals.push_back(i);

    if (e.first == 0)
      lefts.push_back(e);
    else if (e.second == 0)
      rights.push_back(e);
    else
      mids.push_back(e);
  }

  sort(all(mids), [](auto l, auto r) {
    int lf = (l.second - l.first);
    if (lf != 0)
      lf /= abs(l.second - l.first);

    int rf = (r.second - r.first);
    if (rf != 0)
      rf /= abs(r.second - r.first);
    if (lf != rf) {
      return lf > rf;
    }

    if (lf == 1) {
      return l.first < r.first;
    } else {
      return l.second > r.second;
    }

    //        return (l.second - l.first) > (r.second - l.first);
  });
  ll st = 0;
  for (auto e : lefts) {
    //        sum += e.first;
    st += e.second;
  }
  for (auto e : mids) {
    st -= e.first;
    if (st < 0) {
      cout << "No" << endl;
      return;
    }
    st += e.second;
  }
  for (auto e : rights) {
    st -= e.first;
  }

  if (st == 0) {
    cout << "Yes" << endl;
    return;
  }
  cout << "No" << endl;

  //    sort(all(starts), [&](int l, int r){
  //        return tapu_list[l].second > tapu_list[r].second;
  //    });
  //
  //    sort(all(goals), [&](int l, int r){
  //        return tapu_list[l].first > tapu_list[r].first;
  //    });

  //    if(starts.empty() || goals.empty()){
  //        cout << "No" << endl;
  //        return;
  //    }
  //
  //    //
  //    auto check = [&](int l, int r) -> bool{
  //        // ( is plus, ) is minus
  //        // don't under 0
  //
  //        ll sum = 0;
  //        sum = tapu_list[l].second;
  //        for(int i = 0; i < tapu_list.size(); ++i){
  //            if(i == l || i == r){
  //                continue;
  //            }
  //
  //            sum -= tapu_list[i].first;
  //            if(sum < 0) return false;
  //            sum += tapu_list[i].second;
  //        }
  //
  //        sum -= tapu_list[r].first;
  //        return sum == 0;
  //    };
  //
  //    if(starts[0] != goals[0]){
  //        if(check(starts[0], goals[0])){
  //            cout << "Yes" << endl;
  //        }else{
  //            cout << "No" << endl;
  //        }
  //        return;
  //    }
  //
  //    if(starts.size() >= 2 && starts[1] != goals[0] && check(starts[1],
  //    goals[0])){
  //        cout << "Yes" << endl;
  //        return;
  //    }
  //
  //    if(goals.size() >= 2 && starts[0] != goals[1] && check(starts[0],
  //    goals[1])){
  //        cout << "Yes" << endl;
  //        return;
  //    }
  //
  //    cout << "No" << endl;

  //    if(sum == 0 && start && goal && st.size() >= 2){
  //        cout << "Yes" << endl;
  //    }else{
  //        cout << "No" << endl;
  //    }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  //    std::ifstream in("input.txt");
  //    std::cin.rdbuf(in.rdbuf());

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RREP(i, n) for (int i = n - 1; i >= 0; --i)
#define RFOR(i, a, b) for (int i = b - 1; i >= a; --i)

#define all(x) begin(x), end(x)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os << "]";
}

// edit

pair<int, int> kakko(string s) {
  vector<char> st;

  for (auto e : s) {
    if (st.empty()) {
      st.push_back(e);
    } else if (st.back() == '(') {
      if (e == ')') {
        st.pop_back();
      } else {
        st.push_back(e);
      }
    } else {
      st.push_back(e);
    }
  }

  int left = 0, right = 0;
  for (auto e : st) {
    if (e == ')')
      left++;
    else
      right++;
  }

  return make_pair(left, right);
}

void solve() {
  int N;
  cin >> N;

  vector<string> S(N);
  REP(i, N) cin >> S[i];

  vector<pair<int, int>> tapu_list;

  for (auto e : S) {
    tapu_list.push_back(kakko(e));
  }

  ll sum = 0;
  vector<int> starts, goals;
  //    set<int> st;

  sort(all(tapu_list), [](auto l, auto r) {
    if (l.first == 0)
      return true;
    if (r.first == 0)
      return false;
    if (l.second == 0)
      return false;
    if (r.second == 0)
      return true;
    return (l.second - l.first) > (r.second - l.first);
  });

  for (int i = 0; i < tapu_list.size(); ++i) {
    auto e = tapu_list[i];
    sum += e.first;
    sum -= e.second;

    //        if(e.first == 0) start = true;
    //        if(e.second == 0) goal = true;

    //        if(e.first == 0 || e.second == 0){
    //            st.insert(i);
    //        }

    if (e.first == 0)
      starts.push_back(i);
    if (e.second == 0)
      goals.push_back(i);
  }

  sort(all(starts),
       [&](int l, int r) { return tapu_list[l].second > tapu_list[r].second; });

  sort(all(goals),
       [&](int l, int r) { return tapu_list[l].first > tapu_list[r].first; });

  if (starts.empty() || goals.empty()) {
    cout << "No" << endl;
    return;
  }

  //
  auto check = [&](int l, int r) -> bool {
    // ( is plus, ) is minus
    // don't under 0

    ll sum = 0;
    sum = tapu_list[l].second;
    for (int i = 0; i < tapu_list.size(); ++i) {
      if (i == l || i == r) {
        continue;
      }

      sum -= tapu_list[i].first;
      if (sum < 0)
        return false;
      sum += tapu_list[i].second;
    }

    sum -= tapu_list[r].first;
    return sum == 0;
  };

  if (starts[0] != goals[0]) {
    if (check(starts[0], goals[0])) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
    return;
  }

  if (starts.size() >= 2 && starts[1] != goals[0] &&
      check(starts[1], goals[0])) {
    cout << "Yes" << endl;
    return;
  }

  if (goals.size() >= 2 && starts[0] != goals[1] &&
      check(starts[0], goals[1])) {
    cout << "Yes" << endl;
    return;
  }

  cout << "No" << endl;

  //    if(sum == 0 && start && goal && st.size() >= 2){
  //        cout << "Yes" << endl;
  //    }else{
  //        cout << "No" << endl;
  //    }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  //    std::ifstream in("input.txt");
  //    std::cin.rdbuf(in.rdbuf());

  solve();

  return 0;
}