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

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble = boost::multiprecision::cpp_dec_float_100;
using namespace boost::multiprecision;
#endif
#ifdef LOCAL_DEV
void debug_impl() { std::cerr << '\\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
  std::cerr << " " << head << (sizeof...(tail) ? "," : "");
  debug_impl(tail...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    std::cerr << "(" << #__VA_ARGS__ << ") =";                                 \\
    debug_impl(__VA_ARGS__);                                                   \\
  } while (false)
#else
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (false)
#endif
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION                                    \\
  / usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
  dvector() : std::vector<T>() {}
  explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
  dvector(const std::vector<T> &v) : std::vector<T>(v) {}
  dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
  dvector(const std::string::iterator first, const std::string::iterator last)
      : std::vector<T>(first, last) {}
  dvector(const typename std::vector<T>::iterator first,
          const typename std::vector<T>::iterator last)
      : std::vector<T>(first, last) {}
  dvector(const typename std::vector<T>::reverse_iterator first,
          const typename std::vector<T>::reverse_iterator last)
      : std::vector<T>(first, last) {}
  dvector(const typename std::vector<T>::const_iterator first,
          const typename std::vector<T>::const_iterator last)
      : std::vector<T>(first, last) {}
  dvector(const typename std::vector<T>::const_reverse_iterator first,
          const typename std::vector<T>::const_reverse_iterator last)
      : std::vector<T>(first, last) {}
  T &operator[](size_t n) {
    try {
      return this->at(n);
    } catch (const std::exception &e) {
      std::cerr << boost::stacktrace::stacktrace() << '\\n';
      return this->at(n);
    }
  }
  const T &operator[](size_t n) const {
    try {
      return this->at(n);
    } catch (const std::exception &e) {
      std::cerr << boost::stacktrace::stacktrace() << '\\n';
      return this->at(n);
    }
  }
};
} // namespace std
class dbool {
private:
  bool boolvalue;

public:
  dbool() : boolvalue(false) {}
  dbool(bool b) : boolvalue(b) {}
  dbool(const dbool &) = default;
  dbool(dbool &&) = default;
  dbool &operator=(const dbool &) = default;
  dbool &operator=(dbool &&) = default;
  operator bool &() { return boolvalue; }
  operator const bool &() const { return boolvalue; }
};
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::dvector<T> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    s << v[i];
    if (i < v.size() - 1)
      s << "\\t";
  }
  return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
                         const std::dvector<std::dvector<T>> &vv) {
  s << "\\\\\\n";
  for (size_t i = 0; i < vv.size(); ++i) {
    s << vv[i] << "\\n";
  }
  return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    s << v[i];
    if (i < v.size() - 1)
      s << "\\t";
  }
  return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
  s << "{ ";
  for (auto itr = se.begin(); itr != se.end(); ++itr) {
    s << (*itr) << "\\t";
  }
  s << "}";
  return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
  s << "{ ";
  for (auto itr = se.begin(); itr != se.end(); ++itr) {
    s << (*itr) << "\\t";
  }
  s << "}";
  return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
  s << "{ ";
  for (size_t i = 0; i < N; ++i) {
    s << a[i] << "\\t";
  }
  s << "}";
  return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
  s << "{\\n";
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << "\\t" << (*itr).first << " : " << (*itr).second << "\\n";
  }
  s << "}";
  return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
  std::cerr << boost::stacktrace::stacktrace() << '\\n';
  throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
  std::cerr << boost::stacktrace::stacktrace() << '\\n';
  throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
  signal(SIGFPE, catch_SIGFPE);
  signal(SIGSEGV, catch_SIGSEGV);
  return convertedmain();
}
#define main() convertedmain()
#endif
// #define int long long
using ll = long long;
// constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
  std::cout << head << (sizeof...(tail) ? " " : "");
  p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
  for (ll i = 0, N = v.size(); i < N; i++)
    std::cout << v[i] << " \\n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
  return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
  return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

/*-----8<-----template-----8<-----*/

/**
 * @brief a segment tree beats (range {chmin, chmax, add, update} + range {min,
 * max, sum})
 * @docs data_structure/segment_tree_beats.md
 * @see https://codeforces.com/blog/entry/57319
 */
class SegmentTreeBeats {
  // MEMO: values for queries (max, min, lazy_add, and lazy_update) already
  // apply to the current node; but not for children
  typedef struct {
    int64_t max;
    int64_t max_second;
    int max_count;
    int64_t min;
    int64_t min_second;
    int min_count;
    int64_t lazy_add;
    int64_t lazy_update;
    int64_t sum;
  } value_type;

public:
  int n;
  vector<value_type> a;

  SegmentTreeBeats() = default;
  SegmentTreeBeats(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    a.resize(2 * n - 1);
    tag<UPDATE>(0, 0);
  }
  template <class InputIterator>
  SegmentTreeBeats(InputIterator first, InputIterator last) {
    int n_ = std::distance(first, last);
    n = 1;
    while (n < n_)
      n *= 2;
    a.resize(2 * n - 1);
    for (int i = 0; i < n_; i++) {
      tag<UPDATE>(n - 1 + i, *(first + i));
    }
    for (ll i = n_; i < n; i++) {
      tag<UPDATE>(n - 1 + i, 0);
    }
    for (ll i = n - 2; i >= 0; i--) {
      update(i);
    }
  }

  void range_chmin(int l, int r, int64_t value) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    range_apply<CHMIN>(0, 0, n, l, r, value);
  }
  void range_chmax(int l, int r, int64_t value) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    range_apply<CHMAX>(0, 0, n, l, r, value);
  }
  void range_add(int l, int r, int64_t value) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    range_apply<ADD>(0, 0, n, l, r, value);
  }
  void range_update(int l, int r, int64_t value) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    range_apply<UPDATE>(0, 0, n, l, r, value);
  }

  int64_t range_min(int l, int r) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    return range_get<MIN>(0, 0, n, l, r);
  }
  int64_t range_max(int l, int r) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    return range_get<MAX>(0, 0, n, l, r);
  }
  int64_t range_sum(int l, int r) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    return range_get<SUM>(0, 0, n, l, r);
  }

private:
  static constexpr char CHMIN = 0;
  static constexpr char CHMAX = 1;
  static constexpr char ADD = 2;
  static constexpr char UPDATE = 3;
  static constexpr char MIN = 10;
  static constexpr char MAX = 11;
  static constexpr char SUM = 12;

  template <char TYPE>
  void range_apply(int i, int il, int ir, int l, int r, int64_t g) {
    if (ir <= l or r <= il or break_condition<TYPE>(i, g)) {
      // break
    } else if (l <= il and ir <= r and tag_condition<TYPE>(i, g)) {
      tag<TYPE>(i, g);
    } else {
      pushdown(i);
      range_apply<TYPE>(2 * i + 1, il, (il + ir) / 2, l, r, g);
      range_apply<TYPE>(2 * i + 2, (il + ir) / 2, ir, l, r, g);
      update(i);
    }
  }
  template <char TYPE> inline bool break_condition(int i, int64_t g) {
    switch (TYPE) {
    case CHMIN:
      return a[i].max <= g;
    case CHMAX:
      return g <= a[i].min;
    case ADD:
      return false;
    case UPDATE:
      return false;
    default:
      assert(false);
    }
  }
  template <char TYPE> inline bool tag_condition(int i, int64_t g) {
    switch (TYPE) {
    case CHMIN:
      return a[i].max_second < g and g < a[i].max;
    case CHMAX:
      return a[i].min < g and g < a[i].min_second;
    case ADD:
      return true;
    case UPDATE:
      return true;
    default:
      assert(false);
    }
  }
  template <char TYPE> inline void tag(int i, int64_t g) {
    int length = n >> (32 - __builtin_clz(i + 1) - 1);
    if (TYPE == CHMIN) {
      if (a[i].max == a[i].min or g <= a[i].min) {
        tag<UPDATE>(i, g);
        return;
      }
      if (a[i].max != INT64_MIN) {
        a[i].sum -= a[i].max * a[i].max_count;
      }
      a[i].max = g;
      a[i].min_second = std::min(a[i].min_second, g);
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update = std::min(a[i].lazy_update, g);
      }
      a[i].sum += g * a[i].max_count;
    } else if (TYPE == CHMAX) {
      if (a[i].max == a[i].min or a[i].max <= g) {
        tag<UPDATE>(i, g);
        return;
      }
      if (a[i].min != INT64_MAX) {
        a[i].sum -= a[i].min * a[i].min_count;
      }
      a[i].min = g;
      a[i].max_second = std::max(a[i].max_second, g);
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update = std::max(a[i].lazy_update, g);
      }
      a[i].sum += g * a[i].min_count;
    } else if (TYPE == ADD) {
      if (a[i].max != INT64_MAX) {
        a[i].max += g;
      }
      if (a[i].max_second != INT64_MIN) {
        a[i].max_second += g;
      }
      if (a[i].min != INT64_MIN) {
        a[i].min += g;
      }
      if (a[i].min_second != INT64_MAX) {
        a[i].min_second += g;
      }
      a[i].lazy_add += g;
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update += g;
      }
      a[i].sum += g * length;
    } else if (TYPE == UPDATE) {
      a[i].max = g;
      a[i].max_second = INT64_MIN;
      a[i].max_count = length;
      a[i].min = g;
      a[i].min_second = INT64_MAX;
      a[i].min_count = length;
      a[i].lazy_add = 0;
      a[i].lazy_update = INT64_MAX;
      a[i].sum = g * length;
    } else {
      assert(false);
    }
  }
  void pushdown(int i) {
    int l = 2 * i + 1;
    int r = 2 * i + 2;
    // update
    if (a[i].lazy_update != INT64_MAX) {
      tag<UPDATE>(l, a[i].lazy_update);
      tag<UPDATE>(r, a[i].lazy_update);
      a[i].lazy_update = INT64_MAX;
      return;
    }
    // add
    if (a[i].lazy_add != 0) {
      tag<ADD>(l, a[i].lazy_add);
      tag<ADD>(r, a[i].lazy_add);
      a[i].lazy_add = 0;
    }
    // chmin
    if (a[i].max < a[l].max) {
      tag<CHMIN>(l, a[i].max);
    }
    if (a[i].max < a[r].max) {
      tag<CHMIN>(r, a[i].max);
    }
    // chmax
    if (a[l].min < a[i].min) {
      tag<CHMAX>(l, a[i].min);
    }
    if (a[r].min < a[i].min) {
      tag<CHMAX>(r, a[i].min);
    }
  }
  void update(int i) {
    int l = 2 * i + 1;
    int r = 2 * i + 2;
    // chmin
    vector<int64_t> b{a[l].max, a[l].max_second, a[r].max, a[r].max_second};
    std::sort(b.rbegin(), b.rend());
    b.erase(std::unique(b.begin(), b.end()), b.end());
    a[i].max = b[0];
    a[i].max_second = b[1];
    a[i].max_count = (b[0] == a[l].max ? a[l].max_count : 0) +
                     (b[0] == a[r].max ? a[r].max_count : 0);
    // chmax
    vector<int64_t> c{a[l].min, a[l].min_second, a[r].min, a[r].min_second};
    std::sort(c.begin(), c.end());
    c.erase(std::unique(c.begin(), c.end()), c.end());
    a[i].min = c[0];
    a[i].min_second = c[1];
    a[i].min_count = (c[0] == a[l].min ? a[l].min_count : 0) +
                     (c[0] == a[r].min ? a[r].min_count : 0);
    // add
    a[i].lazy_add = 0;
    // update
    a[i].lazy_update = INT64_MAX;
    // sum
    a[i].sum = a[l].sum + a[r].sum;
  }

  template <char TYPE> int64_t range_get(int i, int il, int ir, int l, int r) {
    if (ir <= l or r <= il) {
      return 0;
    } else if (l <= il and ir <= r) {
      // base
      switch (TYPE) {
      case MIN:
        return a[i].min;
      case MAX:
        return a[i].max;
      case SUM:
        return a[i].sum;
      default:
        assert(false);
      }
    } else {
      pushdown(i);
      int64_t value_l = range_get<TYPE>(2 * i + 1, il, (il + ir) / 2, l, r);
      int64_t value_r = range_get<TYPE>(2 * i + 2, (il + ir) / 2, ir, l, r);
      // mult
      switch (TYPE) {
      case MIN:
        return std::min(value_l, value_r);
      case MAX:
        return std::max(value_l, value_r);
      case SUM:
        return value_l + value_r;
      default:
        assert(false);
      }
    }
  }
};
ostream &operator<<(ostream &s, SegmentTreeBeats &seg) {
  for (ll i = 0; i < seg.n; i++)
    s << seg.range_sum(i, i + 1) << "\\t\\n"[i == seg.n - 1];
  return s;
}

/*-----8<-----library-----8<-----*/

constexpr ll N_MAX = 1e6;

ll N;
vector<string> S;

void solve() {
  scanf("%lld", &N);
  S.assign(N, "");
  rep(i, N) { cin >> S[i]; }

  if ([&] {
        ll t = 0;
        ll s = 0;
        rep(i, N) rep(j, S[i].size()) {
          if (S[i][j] == '(')
            t++;
          else
            s++;
        }
        return t != s;
      }()) {
    puts("No");
    return;
  }

  vector<string> u, v;
  string zero = "";
  rep(i, N) {
    ll t = 0;
    ll sum = 0;
    ll chokin = 0;
    rep(j, S[i].size()) {
      if (S[i][j] == '(') {
        sum++;
        chokin++;
      } else {
        sum--;
        if (chokin == 0)
          t++;
        else
          chokin--;
      }
    }
    if (sum == 0) {
      zero += S[i];
    } else if (sum > 0) {
      u.push_back(S[i]);
    } else {
      string x = "";
      rrep(j, S[i].size()) {
        if (S[i][j] == '(')
          x += ')';
        else
          x += '(';
      }
      v.push_back(x);
    }
  }

  auto f = [&](vector<string> &S) -> string {
    ll size = S.size();
    vector<array<ll, 3>> v;
    rep(i, size) {
      ll t = 0;
      ll sum = 0;
      ll chokin = 0;
      rep(j, S[i].size()) {
        if (S[i][j] == '(') {
          sum++;
          chokin++;
        } else {
          sum--;
          if (chokin == 0)
            t++;
          else
            chokin--;
        }
      }
      v.push_back({t, sum, i});
    }
    sort(all(v));
    string ans = "";
    rep(i, v.size()) { ans += S[v[i][2]]; }
    return ans;
  };
  string x = f(u);
  debug(u, x);
  string y = f(v);
  string z = "";
  rrep(j, y.size()) {
    if (y[j] == '(')
      z += ')';
    else
      z += '(';
  }
  debug(z);
  string T = x + zero + z;
  debug(T);
  ll chokin = 0;
  rep(i, T.size()) {
    if (T[i] == '(') {
      chokin++;
    } else {
      if (chokin == 0) {
        puts("No");
        return;
      } else {
        chokin--;
      }
    }
  }
  if (chokin == 0)
    puts("Yes");
  else
    puts("No");
}

// https://atcoder.jp/contests/abc167/tasks/abc167_f
signed main() {
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using bll = boost::multiprecision::cpp_int;
using bdouble = boost::multiprecision::cpp_dec_float_100;
using namespace boost::multiprecision;
#endif
#ifdef LOCAL_DEV
void debug_impl() { std::cerr << '\\n'; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
  std::cerr << " " << head << (sizeof...(tail) ? "," : "");
  debug_impl(tail...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    std::cerr << "(" << #__VA_ARGS__ << ") =";                                 \\
    debug_impl(__VA_ARGS__);                                                   \\
  } while (false)
#else
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (false)
#endif
#ifdef LOCAL_TEST
#define BOOST_STACKTRACE_USE_ADDR2LINE
#define BOOST_STACKTRACE_ADDR2LINE_LOCATION                                    \\
  / usr / local / opt / binutils / bin / addr2line
#define _GNU_SOURCE
#include <boost/stacktrace.hpp>
namespace std {
template <typename T> class dvector : public std::vector<T> {
public:
  dvector() : std::vector<T>() {}
  explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {}
  dvector(const std::vector<T> &v) : std::vector<T>(v) {}
  dvector(const std::initializer_list<T> il) : std::vector<T>(il) {}
  dvector(const std::string::iterator first, const std::string::iterator last)
      : std::vector<T>(first, last) {}
  dvector(const typename std::vector<T>::iterator first,
          const typename std::vector<T>::iterator last)
      : std::vector<T>(first, last) {}
  dvector(const typename std::vector<T>::reverse_iterator first,
          const typename std::vector<T>::reverse_iterator last)
      : std::vector<T>(first, last) {}
  dvector(const typename std::vector<T>::const_iterator first,
          const typename std::vector<T>::const_iterator last)
      : std::vector<T>(first, last) {}
  dvector(const typename std::vector<T>::const_reverse_iterator first,
          const typename std::vector<T>::const_reverse_iterator last)
      : std::vector<T>(first, last) {}
  T &operator[](size_t n) {
    try {
      return this->at(n);
    } catch (const std::exception &e) {
      std::cerr << boost::stacktrace::stacktrace() << '\\n';
      return this->at(n);
    }
  }
  const T &operator[](size_t n) const {
    try {
      return this->at(n);
    } catch (const std::exception &e) {
      std::cerr << boost::stacktrace::stacktrace() << '\\n';
      return this->at(n);
    }
  }
};
} // namespace std
class dbool {
private:
  bool boolvalue;

public:
  dbool() : boolvalue(false) {}
  dbool(bool b) : boolvalue(b) {}
  dbool(const dbool &) = default;
  dbool(dbool &&) = default;
  dbool &operator=(const dbool &) = default;
  dbool &operator=(dbool &&) = default;
  operator bool &() { return boolvalue; }
  operator const bool &() const { return boolvalue; }
};
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::dvector<T> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    s << v[i];
    if (i < v.size() - 1)
      s << "\\t";
  }
  return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
                         const std::dvector<std::dvector<T>> &vv) {
  s << "\\\\\\n";
  for (size_t i = 0; i < vv.size(); ++i) {
    s << vv[i] << "\\n";
  }
  return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    s << v[i];
    if (i < v.size() - 1)
      s << "\\t";
  }
  return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &se) {
  s << "{ ";
  for (auto itr = se.begin(); itr != se.end(); ++itr) {
    s << (*itr) << "\\t";
  }
  s << "}";
  return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) {
  s << "{ ";
  for (auto itr = se.begin(); itr != se.end(); ++itr) {
    s << (*itr) << "\\t";
  }
  s << "}";
  return s;
}
template <typename T, size_t N>
std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) {
  s << "{ ";
  for (size_t i = 0; i < N; ++i) {
    s << a[i] << "\\t";
  }
  s << "}";
  return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) {
  s << "{\\n";
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << "\\t" << (*itr).first << " : " << (*itr).second << "\\n";
  }
  s << "}";
  return s;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
#define vector dvector
#define bool dbool
class SIGFPE_exception : std::exception {};
class SIGSEGV_exception : std::exception {};
void catch_SIGFPE([[maybe_unused]] int e) {
  std::cerr << boost::stacktrace::stacktrace() << '\\n';
  throw SIGFPE_exception();
}
void catch_SIGSEGV([[maybe_unused]] int e) {
  std::cerr << boost::stacktrace::stacktrace() << '\\n';
  throw SIGSEGV_exception();
}
signed convertedmain();
signed main() {
  signal(SIGFPE, catch_SIGFPE);
  signal(SIGSEGV, catch_SIGSEGV);
  return convertedmain();
}
#define main() convertedmain()
#endif
// #define int long long
using ll = long long;
// constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1};
#define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i)
#define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrepeq(i, n) for (ll i = (n); i >= 1; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
void p() { std::cout << '\\n'; }
template <typename Head, typename... Tail> void p(Head head, Tail... tail) {
  std::cout << head << (sizeof...(tail) ? " " : "");
  p(tail...);
}
template <typename T> inline void pv(std::vector<T> &v) {
  for (ll i = 0, N = v.size(); i < N; i++)
    std::cout << v[i] << " \\n"[i == N - 1];
}
template <typename T> inline bool chmax(T &a, T b) {
  return a < b && (a = b, true);
}
template <typename T> inline bool chmin(T &a, T b) {
  return a > b && (a = b, true);
}
template <typename T> inline void uniq(std::vector<T> &v) {
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

/*-----8<-----template-----8<-----*/

/**
 * @brief a segment tree beats (range {chmin, chmax, add, update} + range {min,
 * max, sum})
 * @docs data_structure/segment_tree_beats.md
 * @see https://codeforces.com/blog/entry/57319
 */
class SegmentTreeBeats {
  // MEMO: values for queries (max, min, lazy_add, and lazy_update) already
  // apply to the current node; but not for children
  typedef struct {
    int64_t max;
    int64_t max_second;
    int max_count;
    int64_t min;
    int64_t min_second;
    int min_count;
    int64_t lazy_add;
    int64_t lazy_update;
    int64_t sum;
  } value_type;

public:
  int n;
  vector<value_type> a;

  SegmentTreeBeats() = default;
  SegmentTreeBeats(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    a.resize(2 * n - 1);
    tag<UPDATE>(0, 0);
  }
  template <class InputIterator>
  SegmentTreeBeats(InputIterator first, InputIterator last) {
    int n_ = std::distance(first, last);
    n = 1;
    while (n < n_)
      n *= 2;
    a.resize(2 * n - 1);
    for (int i = 0; i < n_; i++) {
      tag<UPDATE>(n - 1 + i, *(first + i));
    }
    for (ll i = n_; i < n; i++) {
      tag<UPDATE>(n - 1 + i, 0);
    }
    for (ll i = n - 2; i >= 0; i--) {
      update(i);
    }
  }

  void range_chmin(int l, int r, int64_t value) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    range_apply<CHMIN>(0, 0, n, l, r, value);
  }
  void range_chmax(int l, int r, int64_t value) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    range_apply<CHMAX>(0, 0, n, l, r, value);
  }
  void range_add(int l, int r, int64_t value) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    range_apply<ADD>(0, 0, n, l, r, value);
  }
  void range_update(int l, int r, int64_t value) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    range_apply<UPDATE>(0, 0, n, l, r, value);
  }

  int64_t range_min(int l, int r) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    return range_get<MIN>(0, 0, n, l, r);
  }
  int64_t range_max(int l, int r) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    return range_get<MAX>(0, 0, n, l, r);
  }
  int64_t range_sum(int l, int r) { // 0-based, [l, r)
    assert(0 <= l and l <= r and r <= n);
    return range_get<SUM>(0, 0, n, l, r);
  }

private:
  static constexpr char CHMIN = 0;
  static constexpr char CHMAX = 1;
  static constexpr char ADD = 2;
  static constexpr char UPDATE = 3;
  static constexpr char MIN = 10;
  static constexpr char MAX = 11;
  static constexpr char SUM = 12;

  template <char TYPE>
  void range_apply(int i, int il, int ir, int l, int r, int64_t g) {
    if (ir <= l or r <= il or break_condition<TYPE>(i, g)) {
      // break
    } else if (l <= il and ir <= r and tag_condition<TYPE>(i, g)) {
      tag<TYPE>(i, g);
    } else {
      pushdown(i);
      range_apply<TYPE>(2 * i + 1, il, (il + ir) / 2, l, r, g);
      range_apply<TYPE>(2 * i + 2, (il + ir) / 2, ir, l, r, g);
      update(i);
    }
  }
  template <char TYPE> inline bool break_condition(int i, int64_t g) {
    switch (TYPE) {
    case CHMIN:
      return a[i].max <= g;
    case CHMAX:
      return g <= a[i].min;
    case ADD:
      return false;
    case UPDATE:
      return false;
    default:
      assert(false);
    }
  }
  template <char TYPE> inline bool tag_condition(int i, int64_t g) {
    switch (TYPE) {
    case CHMIN:
      return a[i].max_second < g and g < a[i].max;
    case CHMAX:
      return a[i].min < g and g < a[i].min_second;
    case ADD:
      return true;
    case UPDATE:
      return true;
    default:
      assert(false);
    }
  }
  template <char TYPE> inline void tag(int i, int64_t g) {
    int length = n >> (32 - __builtin_clz(i + 1) - 1);
    if (TYPE == CHMIN) {
      if (a[i].max == a[i].min or g <= a[i].min) {
        tag<UPDATE>(i, g);
        return;
      }
      if (a[i].max != INT64_MIN) {
        a[i].sum -= a[i].max * a[i].max_count;
      }
      a[i].max = g;
      a[i].min_second = std::min(a[i].min_second, g);
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update = std::min(a[i].lazy_update, g);
      }
      a[i].sum += g * a[i].max_count;
    } else if (TYPE == CHMAX) {
      if (a[i].max == a[i].min or a[i].max <= g) {
        tag<UPDATE>(i, g);
        return;
      }
      if (a[i].min != INT64_MAX) {
        a[i].sum -= a[i].min * a[i].min_count;
      }
      a[i].min = g;
      a[i].max_second = std::max(a[i].max_second, g);
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update = std::max(a[i].lazy_update, g);
      }
      a[i].sum += g * a[i].min_count;
    } else if (TYPE == ADD) {
      if (a[i].max != INT64_MAX) {
        a[i].max += g;
      }
      if (a[i].max_second != INT64_MIN) {
        a[i].max_second += g;
      }
      if (a[i].min != INT64_MIN) {
        a[i].min += g;
      }
      if (a[i].min_second != INT64_MAX) {
        a[i].min_second += g;
      }
      a[i].lazy_add += g;
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update += g;
      }
      a[i].sum += g * length;
    } else if (TYPE == UPDATE) {
      a[i].max = g;
      a[i].max_second = INT64_MIN;
      a[i].max_count = length;
      a[i].min = g;
      a[i].min_second = INT64_MAX;
      a[i].min_count = length;
      a[i].lazy_add = 0;
      a[i].lazy_update = INT64_MAX;
      a[i].sum = g * length;
    } else {
      assert(false);
    }
  }
  void pushdown(int i) {
    int l = 2 * i + 1;
    int r = 2 * i + 2;
    // update
    if (a[i].lazy_update != INT64_MAX) {
      tag<UPDATE>(l, a[i].lazy_update);
      tag<UPDATE>(r, a[i].lazy_update);
      a[i].lazy_update = INT64_MAX;
      return;
    }
    // add
    if (a[i].lazy_add != 0) {
      tag<ADD>(l, a[i].lazy_add);
      tag<ADD>(r, a[i].lazy_add);
      a[i].lazy_add = 0;
    }
    // chmin
    if (a[i].max < a[l].max) {
      tag<CHMIN>(l, a[i].max);
    }
    if (a[i].max < a[r].max) {
      tag<CHMIN>(r, a[i].max);
    }
    // chmax
    if (a[l].min < a[i].min) {
      tag<CHMAX>(l, a[i].min);
    }
    if (a[r].min < a[i].min) {
      tag<CHMAX>(r, a[i].min);
    }
  }
  void update(int i) {
    int l = 2 * i + 1;
    int r = 2 * i + 2;
    // chmin
    vector<int64_t> b{a[l].max, a[l].max_second, a[r].max, a[r].max_second};
    std::sort(b.rbegin(), b.rend());
    b.erase(std::unique(b.begin(), b.end()), b.end());
    a[i].max = b[0];
    a[i].max_second = b[1];
    a[i].max_count = (b[0] == a[l].max ? a[l].max_count : 0) +
                     (b[0] == a[r].max ? a[r].max_count : 0);
    // chmax
    vector<int64_t> c{a[l].min, a[l].min_second, a[r].min, a[r].min_second};
    std::sort(c.begin(), c.end());
    c.erase(std::unique(c.begin(), c.end()), c.end());
    a[i].min = c[0];
    a[i].min_second = c[1];
    a[i].min_count = (c[0] == a[l].min ? a[l].min_count : 0) +
                     (c[0] == a[r].min ? a[r].min_count : 0);
    // add
    a[i].lazy_add = 0;
    // update
    a[i].lazy_update = INT64_MAX;
    // sum
    a[i].sum = a[l].sum + a[r].sum;
  }

  template <char TYPE> int64_t range_get(int i, int il, int ir, int l, int r) {
    if (ir <= l or r <= il) {
      return 0;
    } else if (l <= il and ir <= r) {
      // base
      switch (TYPE) {
      case MIN:
        return a[i].min;
      case MAX:
        return a[i].max;
      case SUM:
        return a[i].sum;
      default:
        assert(false);
      }
    } else {
      pushdown(i);
      int64_t value_l = range_get<TYPE>(2 * i + 1, il, (il + ir) / 2, l, r);
      int64_t value_r = range_get<TYPE>(2 * i + 2, (il + ir) / 2, ir, l, r);
      // mult
      switch (TYPE) {
      case MIN:
        return std::min(value_l, value_r);
      case MAX:
        return std::max(value_l, value_r);
      case SUM:
        return value_l + value_r;
      default:
        assert(false);
      }
    }
  }
};
ostream &operator<<(ostream &s, SegmentTreeBeats &seg) {
  for (ll i = 0; i < seg.n; i++)
    s << seg.range_sum(i, i + 1) << "\\t\\n"[i == seg.n - 1];
  return s;
}

/*-----8<-----library-----8<-----*/

constexpr ll N_MAX = 1e6;

ll N;
vector<string> S;

void solve() {
  scanf("%lld", &N);
  S.assign(N, "");
  rep(i, N) { cin >> S[i]; }

  if ([&] {
        ll t = 0;
        ll s = 0;
        rep(i, N) rep(j, S[i].size()) {
          if (S[i][j] == '(')
            t++;
          else
            s++;
        }
        return t != s;
      }()) {
    puts("No");
    return;
  }

  vector<pair<ll, ll>> u, v;
  ll zero = 0;
  rep(i, N) {
    ll t = 0;
    ll sum = 0;
    ll chokin = 0;
    rep(j, S[i].size()) {
      if (S[i][j] == '(') {
        sum++;
        chokin++;
      } else {

        if (chokin <= 0)
          t++;
        sum--;
        if (chokin > 0)
          chokin--;
      }
    }
    debug(t, sum);
    if (sum == 0) {
      chmax(zero, t);
    } else if (sum > 0) {
      u.push_back({t, sum});

    } else {
      // sum=ダメージ
      // t 要求(
      v.push_back({-t, -sum});
    }
  }

  ll ima = 0;
  sort(all(u));
  rep(i, u.size()) {
    if (ima < u[i].first) {
      puts("No");
      return;
    }
    ima += u[i].second;
  }

  if (ima < zero) {
    puts("No");
    return;
  }

  sort(all(v));
  debug(v);
  debug(ima);
  rep(i, v.size()) {
    v[i].first *= -1;
    if (ima < v[i].first) {
      // puts("No");return;
      exit(1);
    }
    ima -= v[i].second;
  }

  puts("Yes");
}

// https://atcoder.jp/contests/abc167/tasks/abc167_f
signed main() {
  solve();
  return 0;
}