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

#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;

int main() {
  int N;
  cin >> N;
  vector<P> inc, dec, keep;
  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    int now = 0, lowest = 0, end;
    for (int j = 0; j < s.size(); ++j) {
      if (s[j] == '(')
        ++now;
      else
        --now;
      end = now;
      lowest = min(lowest, now);
    }
    if (end > 0)
      inc.push_back(P(lowest, end));
    else if (end < 0)
      dec.push_back(P(lowest - end, end));
    else if (end == 0)
      keep.push_back(P(lowest, end));
  }

  sort(inc.begin(), inc.end(), greater<P>());
  sort(dec.begin(), dec.end());

  // DUMP(inc, keep, dec);

  int now = 0;
  for (P e : inc) {
    int lowest = now + e.first;
    if (lowest < 0) {
      cout << "No" << endl;
      return 0;
    }
    now += e.second;
  }
  for (P e : keep) {
    int lowest = now + e.first;
    if (lowest < 0) {
      cout << "No" << endl;
      return 0;
    }
    now += e.second;
  }
  for (P e : dec) {
    int lowest = now + e.second + e.first;
    if (lowest < 0) {
      cout << "No" << endl;
      return 0;
    }
    now += e.second;
  }
  if (now == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

class Debug {
private:
  static int NAME_LENGTH, MAX_WIDTH;
  static bool IS_MATRIX;
  static long long INF;

public:
  static void set_NAME_LENGTH(int l) { NAME_LENGTH = l; }
  static int get_NAME_LENGTH() { return NAME_LENGTH; }
  static void set_MAX_WIDTH(int w) { MAX_WIDTH = w; }
  static int get_MAX_WIDTH() { return MAX_WIDTH; }
  static void set_IS_MATRIX(bool f) { IS_MATRIX = f; }
  static bool get_IS_MATRIX() { return IS_MATRIX; }
  static void set_INF(long long i) { INF = i; }
  static long long get_INF() { return INF; }
};

int Debug::NAME_LENGTH = 0;
int Debug::MAX_WIDTH = 0;
bool Debug::IS_MATRIX = false;
long long Debug::INF = LLONG_MAX;

// Debug for basic type element
template <typename T> int ELEMENT_WIDTH(const T &e) {
  int width;
  stringstream ss;
  ss << e;
  width = ss.str().size();
  if (ss.str() == to_string(Debug::get_INF()))
    width = "INF"s.size();
  if (ss.str() == to_string(-Debug::get_INF()))
    width = "-INF"s.size();
  return width;
}

template <typename T> ostream &operator<<(ostream &os, const T *e) {
  stringstream ss;
  ss << *e;
  if (ss.str() == to_string(Debug::get_INF()))
    os << "INF"s;
  else if (ss.str() == to_string(-Debug::get_INF()))
    os << "-INF"s;
  else
    os << ss.str();
  return os;
}

ostream &operator<<(ostream &os, const char *e) {
  string s = e;
  os << s;
  return os;
}

// Debug for pair
template <typename T, typename U> int ELEMENT_WIDTH(const pair<T, U> &p) {
  return ELEMENT_WIDTH(p.first) + ELEMENT_WIDTH(p.second) + "(, )"s.size();
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> *p) {
  os << "("s << &((*p).first) << ", "s << &((*p).second) << ")"s;
  return os;
}

// Debug for tuple
template <int N, class Tuple> int WIDTH_TUPLE(const Tuple &t) { return 0; }
template <int N, class Tuple, class Head, class... Tail>
int WIDTH_TUPLE(const Tuple &t) {
  int width = 0;
  if (N)
    width += ", "s.size();
  width += ELEMENT_WIDTH(get<N>(t)) + WIDTH_TUPLE<N + 1, Tuple, Tail...>(t);
  return width;
}
template <class... Ts> int ELEMENT_WIDTH(const tuple<Ts...> &t) {
  return WIDTH_TUPLE<0, tuple<Ts...>, Ts...>(t) + "()"s.size();
}

template <int N, class Tuple> void OUT_TUPLE(ostream &os, const Tuple *t) {}
template <int N, class Tuple, class Head, class... Tail>
void OUT_TUPLE(ostream &os, const Tuple *t) {
  if (N)
    os << ", "s;
  os << &get<N>(*t);
  OUT_TUPLE<N + 1, Tuple, Tail...>(os, &(*t));
}
template <class... Ts> ostream &operator<<(ostream &os, const tuple<Ts...> *t) {
  os << "("s;
  OUT_TUPLE<0, tuple<Ts...>, Ts...>(os, &(*t));
  os << ")"s;
  return os;
}

// Debug for vector (1-dimensional)
template <typename T> ostream &operator<<(ostream &os, const vector<T> *v) {
  os << "["s;
  bool flg = true;
  for (auto e : *v) {
    os << (flg ? ""s : ", "s);
    if (Debug::get_IS_MATRIX()) {
      string space = "";
      for (int i = 0; i < Debug::get_MAX_WIDTH() - ELEMENT_WIDTH(e); ++i)
        space += " ";
      os << space;
    }
    os << &e;
    flg = false;
  }
  os << "]"s;
  return os;
}

// Debug for vector (2-dimensional)
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> *v) {
  Debug::set_IS_MATRIX(true);
  int width = 0;
  for (unsigned int i = 0; i < (*v).size(); ++i) {
    for (unsigned int j = 0; j < ((*v)[i]).size(); ++j) {
      width = max(width, ELEMENT_WIDTH((*v)[i][j]));
    }
  }
  Debug::set_MAX_WIDTH(width);
  string space = "";
  for (int i = 0; i < Debug::get_NAME_LENGTH() + 7; ++i)
    space += " ";
  os << "["s;
  bool flg = true;
  for (auto e : *v) {
    os << (flg ? ""s : "\\n" + space);
    os << &e;
    flg = false;
  }
  os << "]"s;
  Debug::set_IS_MATRIX(false);
  return os;
}

// Debug for set
template <typename T> ostream &operator<<(ostream &os, const set<T> *s) {
  os << "{"s;
  bool flg = true;
  for (auto e : *s) {
    os << (flg ? ""s : ", "s);
    os << &e;
    flg = false;
  }
  os << "}"s;
  return os;
}

// Debug for multiset
template <typename T> ostream &operator<<(ostream &os, const multiset<T> *s) {
  os << "{"s;
  bool flg = true;
  for (auto e : *s) {
    os << (flg ? ""s : ", "s);
    os << &e;
    flg = false;
  }
  os << "}"s;
  return os;
}

// Debug for map
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> *mp) {
  os << "{"s;
  bool flg = true;
  for (auto e : *mp) {
    os << (flg ? ""s : ", "s);
    os << &(e.first) << ": "s << &(e.second);
    flg = false;
  }
  os << "}"s;
  return os;
}

// Debug for queue
template <typename T> ostream &operator<<(ostream &os, const queue<T> *que) {
  queue<T> que_cp = *que;
  os << "["s;
  bool flg = true;
  while (!que_cp.empty()) {
    os << (flg ? ""s : ", "s);
    os << &(que_cp.front());
    que_cp.pop();
    flg = false;
  }
  os << "]"s;
  return os;
}

// Debug for deque
template <typename T> ostream &operator<<(ostream &os, const deque<T> *que) {
  os << "["s;
  bool flg = true;
  for (auto e : *que) {
    os << (flg ? ""s : ", "s);
    os << &e;
    flg = false;
  }
  os << "]"s;
  return os;
}

// Debug assistant
#define DUMP(...) INTERNAL_DUMP(#__VA_ARGS__, __VA_ARGS__)

void REMOVE_SPACE(string &s) {
  while (s[0] == ' ') {
    s = s.substr(1);
  }
  while (s[s.size() - 1] == ' ') {
    s = s.substr(0, s.size() - 1);
  }
}

pair<string, string> GET_NAME(string s) {
  int bracket = 0, idx = 0;
  while ((unsigned)idx < s.size() && !(s[idx] == ',' && bracket == 0)) {
    if (s[idx] == '(')
      ++bracket;
    else if (s[idx] == ')')
      --bracket;
    ++idx;
  }
  string other_names;
  if ((unsigned)idx == s.size()) {
    other_names = "";
  } else {
    other_names = s.substr(idx + 1);
  }
  string first_name = s.substr(0, idx);
  REMOVE_SPACE(first_name);
  REMOVE_SPACE(other_names);
  return pair<string, string>(first_name, other_names);
}

// Debug main
void INTERNAL_DUMP(string name) {}
template <class Head, class... Tail>
void INTERNAL_DUMP(string name, Head &&head, Tail &&...tail) {
  pair<string, string> p = GET_NAME(name);
  Debug::set_NAME_LENGTH(p.first.size());
  cerr << ">> "s << &(p.first) << " = "s << &head << endl;
  INTERNAL_DUMP(p.second, move(tail)...);
}

// End of Debug parts
//------------------------------------------------------------------------------

bool match(char s, char t) { return s == '(' && t == ')'; }

int main() {
  int N;
  cin >> N;
  int cnt_are = 0, cnt_left = 0, cnt_right = 0;
  for (int i = 0; i < N; ++i) {
    stack<char> s;
    string t;
    cin >> t;
    for (int j = 0; j < t.size(); ++j) {
      if (!s.empty() && match(s.top(), t[j]))
        s.pop();
      else
        s.push(t[j]);
    }
    bool f = false;
    // DUMP("called");
    if (s.size() == 2) {
      char u = s.top();
      if (u != '(') {
        f = false;
        break;
      }
      s.pop();
      u = s.top();
      if (u != ')') {
        f = false;
        break;
      }
      f = true;
    }
    if (f)
      ++cnt_are;
    else {
      char u = s.top();
      if (u == '(')
        cnt_left += s.size();
      else
        cnt_right += s.size();
    }
  }
  // DUMP(cnt_are, cnt_left, cnt_right);

  if (cnt_left > cnt_right) {
    cout << "No" << endl;
    return 0;
  }
  if (cnt_are > 0) {
    if (cnt_left == 0 || cnt_right == 0) {
      cout << "No" << endl;
      return 0;
    }
    if (cnt_left != cnt_right) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}