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

#include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;

// @@ !! LIM(debug cmpNaive perm)
// --> f:<< debug cmpNaive perm
// ---- inserted function << from util.cc
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")";
  return os;
}

template <typename T1, typename T2, typename T3, typename T4>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", "
     << get<3>(t) << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << ']';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const set<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const unordered_set<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const multiset<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T1, typename T2, typename C>
ostream &operator<<(ostream &os, const map<T1, T2, C> &mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

template <typename T1, typename T2, typename C>
ostream &operator<<(ostream &os, const unordered_map<T1, T2, C> &mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

template <typename T, typename T2>
ostream &operator<<(ostream &os, const queue<T, T2> &orig) {
  queue<T, T2> que(orig);
  bool first = true;
  os << '[';
  while (!que.empty()) {
    T x = que.front();
    que.pop();
    if (!first)
      os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}

template <typename T, typename T2, typename T3>
ostream &operator<<(ostream &os, const priority_queue<T, T2, T3> &orig) {
  priority_queue<T, T2, T3> pq(orig);
  bool first = true;
  os << '[';
  while (!pq.empty()) {
    T x = pq.top();
    pq.pop();
    if (!first)
      os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}

template <typename T> ostream &operator<<(ostream &os, const stack<T> &st) {
  stack<T> tmp(st);
  os << '[';
  bool first = true;
  while (!tmp.empty()) {
    T &t = tmp.top();
    if (first)
      first = false;
    else
      os << ", ";
    os << t;
    tmp.pop();
  }
  os << ']';
  return os;
}

#if __cplusplus >= 201703L
template <typename T> ostream &operator<<(ostream &os, const optional<T> &t) {
  if (t.has_value())
    os << "v(" << t.value() << ")";
  else
    os << "nullopt";
  return os;
}
#endif

ostream &operator<<(ostream &os, int8_t x) {
  os << (int32_t)x;
  return os;
}

// ---- end <<
// ---- inserted library file debug.cc
template <class... Args> string dbgFormat(const char *fmt, Args... args) {
  size_t len = snprintf(nullptr, 0, fmt, args...);
  char buf[len + 1];
  snprintf(buf, len + 1, fmt, args...);
  return string(buf);
}

template <class Head> void dbgLog(Head &&head) { cerr << head << endl; }

template <class Head, class... Tail> void dbgLog(Head &&head, Tail &&...tail) {
  cerr << head << " ";
  dbgLog(forward<Tail>(tail)...);
}

#if DEBUG
#define DLOG(...) dbgLog(__VA_ARGS__)
#define DFMT(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL(func, ...) func(__VA_ARGS__)
#else
#define DLOG(...)
#define DFMT(...)
#define DCALL(func, ...)
#endif

#if DEBUG_LIB
#define DLOG_LIB(...) dbgLog(__VA_ARGS__)
#define DFMT_LIB(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL_LIB(func, ...) func(__VA_ARGS__)
#else
#define DLOG_LIB(...)
#define DFMT_LIB(...)
#define DCALL_LIB(func, ...)
#endif

// ---- end debug.cc
// ---- inserted library file cmpNaive.cc

const string end_mark("^__=end=__^");
const string end_file_mark("^__=end_file=__^");

int naive(istream &cin, ostream &cout);
int body(istream &cin, ostream &cout);

void cmpNaive() {
  while (true) {
    string s;
    getline(cin, s);
    bool run_body;
    if (s.at(0) == 'Q') {
      return;
    } else if (s.at(0) == 'B') {
      run_body = true;
    } else if (s.at(0) == 'N') {
      run_body = false;
    } else {
      cerr << "Unknown body/naive specifier.\\n";
      exit(1);
    }
    string input_s;
    while (true) {
      getline(cin, s);
      if (s == end_mark)
        break;
      input_s += s;
      input_s += "\\n";
    }
    stringstream ss_in(move(input_s));
    stringstream ss_out;
    if (run_body) {
      body(ss_in, ss_out);
    } else {
      naive(ss_in, ss_out);
    }
    cout << ss_out.str() << end_mark << endl;
  }
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

#if CMPNAIVE
  if (argc == 2) {
    if (strcmp(argv[1], "cmpNaive") == 0) {
      cmpNaive();
    } else if (strcmp(argv[1], "naive") == 0) {
      naive(cin, cout);
    } else {
      cerr << "Unknown argument.\\n";
      exit(1);
    }
  } else {
#endif
    body(cin, cout);
#if CMPNAIVE
  }
#endif
  return 0;
}

/*
int naive(istream& cin, ostream& cout) {
  return 0;
}
int body(istream& cin, ostream& cout) {
  return 0;
}
*/

// ---- end cmpNaive.cc
// ---- inserted library file perm.cc

template <typename T> class IterPerm {
  int cand_size;
  int seq_size;
  T perm;
  vector<T> cands;
  vector<int> idx;

  void construct(const T &orig, int ss) {
    cand_size = orig.size();
    seq_size = ss;
    perm = T(orig.begin(), orig.begin() + seq_size);
    cands = vector<T>(seq_size);
    if (seq_size > 0)
      cands.at(0) = orig;
    idx = vector<int>(seq_size);
    init();
  }

  void init() {
    for (int i = 0; i < seq_size; i++) {
      perm.at(i) = cands.at(0).at(i);
      if (i > 0) {
        cands.at(i) = T(cands.at(0).begin() + i, cands.at(0).end());
      }
      idx.at(i) = 0;
    }
  }

  // IntPerm is implemented using IterPerm<vector<int>>, and
  // it needs a constructor without parameter.
  friend class IntPerm;
  IterPerm() {}

public:
  IterPerm(const T &orig, int ss) { construct(orig, ss); }
  IterPerm(const T &orig) { construct(orig, orig.size()); }

  const T &get() { return perm; }

  bool next() {
    int i = seq_size - 1;
    while (i >= 0 && ++idx.at(i) >= cand_size - i)
      i--;
    if (i < 0) {
      init();
      return false;
    }
    perm.at(i) = cands.at(i).at(idx.at(i));
    for (int k = i + 1; k < seq_size; k++) {
      for (int m = 0; m < cand_size - k; m++) {
        int shift = m < idx.at(k - 1) ? 0 : 1;
        cands.at(k).at(m) = cands.at(k - 1).at(m + shift);
      }
      idx.at(k) = 0;
      perm.at(k) = cands.at(k).at(0);
    }
    return true;
  }
};

class IntPerm {

  IterPerm<vector<int>> itp;

  void construct(int ns, int ss) {
    vector<int> vec(ns);
    iota(vec.begin(), vec.end(), 0);
    itp = IterPerm(vec, ss);
  }

public:
  IntPerm(int ns, int ss) { construct(ns, ss); }
  IntPerm(int ns) { construct(ns, ns); }
  const vector<int> &get() { return itp.get(); }
  bool next() { return itp.next(); }
};

template <typename T> class IterComb {

  vector<T> orig;
  int c_size;
  vector<T> comb;
  vector<int> idx;

  void init() {
    for (int i = 0; i < c_size; i++) {
      idx.at(i) = i;
      comb.at(i) = orig.at(i);
    }
  }

public:
  IterComb() {}

  IterComb(vector<T> orig_, int c_size_)
      : orig(orig_), c_size(c_size_), comb(c_size), idx(c_size) {
    init();
  }

  const vector<T> &get() { return comb; }

  bool next() {
    int n = orig.size();
    int i = c_size - 1;
    while (i >= 0 && ++idx.at(i) > n - (c_size - i))
      i--;
    if (i < 0) {
      init();
      return false;
    }

    int k = idx.at(i);
    for (; i < c_size; i++, k++) {
      idx.at(i) = k;
      comb.at(i) = orig.at(k);
    }
    return true;
  }
};

class IntComb {

  IterComb<int> itc;

public:
  IntComb(int n, int r) {
    vector<int> seq(n);
    iota(seq.begin(), seq.end(), 0);
    itc = IterComb<int>(seq, r);
  }
  const vector<int> &get() { return itc.get(); }
  bool next() { return itc.next(); }
};

// ---- end perm.cc
// @@ !! LIM  -- end mark --

int naive(istream &cin, ostream &cout) {
  ll N;
  cin >> N;
  vector<string> S(N);
  for (ll i = 0; i < N; i++)
    cin >> S[i];
  IntPerm ip(N);
  const auto &v = ip.get();
  do {
    ll ht = 0;
    bool succ = true;
    for (ll i = 0; i < N; i++) {
      for (char c : S[v[i]]) {
        ht += c == '(' ? 1 : -1;
        if (ht < 0) {
          succ = false;
          break;
        }
      }
      if (!succ)
        break;
    }
    if (succ) {
      if (ht == 0) {
        cout << "Yes\\n";
      } else {
        cout << "No\\n";
      }
      return 0;
    }

  } while (ip.next());
  cout << "No\\n";

  return 0;
}

int body(istream &cin, ostream &cout) {

  ll N;
  cin >> N;
  using sta = pair<ll, ll>;
  vector<sta> vecL;
  vector<sta> vecR;
  ll levD = 0;
  for (ll i = 0; i < N; i++) {
    string s;
    cin >> s;
    ll n = s.size();
    ll depth = 0;
    ll diff = 0;
    for (ll j = 0; j < n; j++) {
      diff += s[j] == '(' ? 1 : -1;
      depth = min(depth, diff);
    }
    if (diff == 0) {
      levD = max(levD, -depth);
    } else if (diff > 0) {
      vecL.emplace_back(-depth, diff);
    } else {
      vecR.emplace_back(diff - depth, -diff);
    }
  }
  sort(vecL.begin(), vecL.end());
  sort(vecR.begin(), vecR.end());
  DLOG("vecL=", vecL);
  DLOG("vecR=", vecR);

  bool failed = false;

  auto check = [&](const auto &vec) -> ll {
    ll cur = 0;
    for (auto [d, f] : vec) {
      if (cur - d < 0) {
        failed = true;
        return 0;
      }
      cur += f;
    }
    return cur;
  };

  ll htL = check(vecL);
  ll htR = check(vecR);
  if (!failed && htL == htR && htL >= levD) {
    cout << "Yes\\n";
  } else {
    cout << "No\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;

// @@ !! LIM(debug)
// --> f:<< debug
// ---- inserted function << from util.cc
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")";
  return os;
}

template <typename T1, typename T2, typename T3, typename T4>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", "
     << get<3>(t) << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << ']';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const set<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const unordered_set<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const multiset<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T1, typename T2, typename C>
ostream &operator<<(ostream &os, const map<T1, T2, C> &mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

template <typename T1, typename T2, typename C>
ostream &operator<<(ostream &os, const unordered_map<T1, T2, C> &mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

template <typename T, typename T2>
ostream &operator<<(ostream &os, const queue<T, T2> &orig) {
  queue<T, T2> que(orig);
  bool first = true;
  os << '[';
  while (!que.empty()) {
    T x = que.front();
    que.pop();
    if (!first)
      os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}

template <typename T, typename T2, typename T3>
ostream &operator<<(ostream &os, const priority_queue<T, T2, T3> &orig) {
  priority_queue<T, T2, T3> pq(orig);
  bool first = true;
  os << '[';
  while (!pq.empty()) {
    T x = pq.top();
    pq.pop();
    if (!first)
      os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}

template <typename T> ostream &operator<<(ostream &os, const stack<T> &st) {
  stack<T> tmp(st);
  os << '[';
  bool first = true;
  while (!tmp.empty()) {
    T &t = tmp.top();
    if (first)
      first = false;
    else
      os << ", ";
    os << t;
    tmp.pop();
  }
  os << ']';
  return os;
}

#if __cplusplus >= 201703L
template <typename T> ostream &operator<<(ostream &os, const optional<T> &t) {
  if (t.has_value())
    os << "v(" << t.value() << ")";
  else
    os << "nullopt";
  return os;
}
#endif

ostream &operator<<(ostream &os, int8_t x) {
  os << (int32_t)x;
  return os;
}

// ---- end <<
// ---- inserted library file debug.cc
template <class... Args> string dbgFormat(const char *fmt, Args... args) {
  size_t len = snprintf(nullptr, 0, fmt, args...);
  char buf[len + 1];
  snprintf(buf, len + 1, fmt, args...);
  return string(buf);
}

template <class Head> void dbgLog(Head &&head) { cerr << head << endl; }

template <class Head, class... Tail> void dbgLog(Head &&head, Tail &&...tail) {
  cerr << head << " ";
  dbgLog(forward<Tail>(tail)...);
}

#if DEBUG
#define DLOG(...) dbgLog(__VA_ARGS__)
#define DFMT(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL(func, ...) func(__VA_ARGS__)
#else
#define DLOG(...)
#define DFMT(...)
#define DCALL(func, ...)
#endif

#if DEBUG_LIB
#define DLOG_LIB(...) dbgLog(__VA_ARGS__)
#define DFMT_LIB(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL_LIB(func, ...) func(__VA_ARGS__)
#else
#define DLOG_LIB(...)
#define DFMT_LIB(...)
#define DCALL_LIB(func, ...)
#endif

// ---- end debug.cc
// @@ !! LIM  -- end mark --

int main(/* int argc, char *argv[] */) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

  DLOG("0:");
  ll N;
  cin >> N;
  using sta = pair<ll, ll>;
  vector<sta> vec;
  vector<string> ss(N);
  for (ll i = 0; i < N; i++) {
    string s;
    cin >> s;
    ss[i] = s;
    ll n = s.size();
    ll depth = 0;
    ll diff = 0;
    for (ll j = 0; j < n; j++) {
      diff += s[j] == '(' ? 1 : -1;
      depth = min(depth, diff);
    }
    vec.emplace_back(diff, depth);
  }
  auto comp = [&](sta x, sta y) -> bool {
    auto [xf, xp] = x;
    auto [yf, yp] = y;
    if (xf != yf)
      return xf > 0 || (xf == 0 && yf < 0);
    if (xf > 0)
      return xp > yp;
    if (xf < 0)
      return xp < yp;
    return false;
  };
  sort(vec.begin(), vec.end(), comp);
  DLOG("vec=", vec);
  ll ht = 0;
  for (auto [f, p] : vec) {
    if (ht + p < 0) {
      cout << "No\\n";
      return 0;
    }
    ht += f;
  }
  if (ht != 0) {
    cout << "No\\n";
  } else {
    cout << "Yes\\n";
  }

  return 0;
}