Problem:
Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)


#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};



template <class T>
class segment_tree {
public:
  using value_type = typename T::value_type;
  using effector_type = typename T::effector_type;
  using value_operation = typename T::value_operation;
  using merge_operation = typename T::merge_operation;

private:
  int size;
  const value_operation op1;
  const merge_operation op2;
  std::vector<value_type> node;

  void update() {
    for (int k = size - 1; k > 0; --k) {
      node[k] = op1(node[k << 1 | 0], node[k << 1 | 1]);
    }
  }

public:
  segment_tree(): op1(value_operation()), op2(merge_operation()) { }
  segment_tree(int size_, const value_type &initial_ = value_operation().identity):
    op1(value_operation()), op2(merge_operation())
  { init(size_, initial_); }
  segment_tree(const std::vector<value_type> &node_):
    op1(value_operation()), op2(merge_operation())
  { build(node_); }

  void init(int size_, const value_type &initial_ = value_operation().identity) {
    size = 1;
    while (size < size_) {
      size <<= 1;
    }
    node.assign(size << 1, initial_);
    update();
  }
  void build(const std::vector<value_type> &node_) {
    init(node_.size());
    assign(node_.begin(), node_.end(), 0);
  }

  void modify(int i, const effector_type &x) {
    i += size;
    node[i] = op2(node[i], x);
    while (i > 1) {
      i >>= 1;
      node[i] = op1(node[i << 1 | 0], node[i << 1 | 1]);
    }
  }
  template <class U>
  void modify(U begin, U end, int i) {
    i += size;
    while (begin != end) {
      node[i] = op2(node[i], *begin);
      ++i;
      ++begin;
    }
    update();
  }

  void assign(int i, const value_type &x) {
    i += size;
    node[i] = x;
    while (i > 1) {
      i >>= 1;
      node[i] = op1(node[i << 1 | 0], node[i << 1 | 1]);
    } 
  }
  template <class U>
  void assign(U begin, U end, int i) {
    i += size;
    while (begin != end) {
      node[i] = *begin;
      ++i;
      ++begin;
    }
    update();
  }

  value_type operator [] (int i) const { 
    return node[i + size];
  }
  value_type fold(int l, int r) const {
    l += size;
    r += size;
    value_type resl = op1.identity;
    value_type resr = op1.identity;
    while (l < r) {
      if (l & 1) {
        resl = op1(resl, node[l++]);
      }
      if (r & 1) {
        resr = op1(node[--r], resr);
      }
      l >>= 1;
      r >>= 1;
    }
    return op1(resl, resr);
  }

};

constexpr int inf = (1 << 30) - 1;

struct monoid {
  using value_type = std::pair<int, int>;
  using effector_type = std::pair<int, int>;
  struct value_operation {
    value_type identity = { -inf, -1 };
    value_type operator () (const value_type &x, const value_type &y) const {
      return std::max(x, y);
    }
  };
  struct merge_operation {
    value_type operator () (const value_type &x, const effector_type &y) const {
      return y;
    }
  };
};


int main() {
  int N;
  std::cin >> N;
  std::vector<std::string> S(N);
  std::vector<int> sum(N), min(N);
  for (int i: range(0, N)) {
    std::cin >> S[i];
    for (auto c: S[i]) {
      if (c == '(') {
        ++sum[i];
      }
      else {
        --sum[i];
        chmin(min[i], sum[i]);
      }
    }
  }
  if (std::accumulate(sum.begin(), sum.end(), 0) != 0) {
    std::cout << "No\n";
    return 0;
  }
  segment_tree<monoid> seg(N);
  {
    std::vector<int> order(N);
    std::iota(order.begin(), order.end(), 0);
    std::sort(order.begin(), order.end(), [&](int i, int j) {
      return min[i] < min[j];
    });
    std::vector<std::pair<int, int>> build(N);
    for (int i: range(0, N)) {
      build[i] = { sum[order[i]], i };
    }
    seg.build(build);
  }
  std::sort(min.begin(), min.end());
  int cur = 0;
  for (int i: range(0, N)) {
    int idx = std::lower_bound(min.begin(), min.end(), -cur) - min.begin();
    auto fold = seg.fold(idx, N);
    if (fold.second < 0) {
      std::cout << "No\n";
      return 0;
    }
    cur += fold.first;
    seg.assign(fold.second, { -inf, -1 });
    if (cur < 0) {
      std::cout << "No\n";
      return 0;
    }
  }
  std::cout << "Yes\n";
  return 0;
}