Language: C++

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

template <typename T>
class SegmentTree {
  const T unit;
  const function<T(const T&, const T&)> op;
  vector<T> container;

 public:
  template <typename Op>
  SegmentTree(int size, const T& identity, const Op& operation)
      : unit(identity), op(operation), container(msb(size * 2 - 1)) {
    fill(container.begin(), container.end(), unit);
  }
  void update(size_t index, T value) {
    int n = container.size() / 2;
    container[index + n] = value;

    int node = (index + n) >> 1;
    while (node > 0) {
      container[node] = op(container[node << 1], container[(node << 1) + 1]);
      node >>= 1;
    }
  }
  T find(size_t s, size_t t) {
    if (s >= t) {
      return unit;
    }
    return find_inner(s, t, 0, container.size() / 2);
  }

 private:
  T find_inner(size_t s, size_t t, size_t lb, size_t ub) {
    if (s == lb && t == ub) {
      int n = container.size() / 2;
      return container[lca(n + s, n + t - 1)];
    }

    T result = unit;
    size_t mid = (lb + ub) / 2;
    if (mid > s) {
      result = op(result, find_inner(s, min(mid, t), lb, mid));
    }
    if (mid < t) {
      result = op(result, find_inner(max(s, mid), t, mid, ub));
    }

    return result;
  }
  int lca(int n0, int n1) {
    int a = n0 ^ n1;
    return n0 / msb(a);
  }
  int msb(int x) {
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;

    return x + 1;
  }
};

int main() {
  int n;
  cin >> n;

  using P = pair<int, int>;
  vector<P> a(n);
  rep(i, n) {
    string s;
    cin >> s;

    int close = 0, open = 0;
    rep(i, s.size()) {
      if (s[i] == ')') {
        if (open == 0) {
          close++;
        } else {
          open--;
        }
      } else {
        open++;
      }
    }

    a[i] = P(close, open - close);
  }
  sort(a.begin(), a.end());

  using Q = pair<int, size_t>;
  const int MAXLEN = 1'000'000 + 1;
  SegmentTree<Q> seg(n, Q(-MAXLEN, -1),
                     [](const int& lh, const int& rh) { return max(lh, rh); });

  rep(i, n) {
    int x, z;
    tie(x, z);
    seg.update(i, Q(z, i));
  }

  int total = 0;
  rep(i, n) {
    size_t idx = upper_bound(a.begin(), a.end(), P(total, MAXLEN)) - a.begin();
    int z;
    size_t j;
    tie(z, j) = seg.find(0, idx);
    if (j == -1) {
      cout << "No" << endl;
      return 0;
    }
    total += z;
    seg.update(j, Q(-MAXLEN, -1));
  }
  cout << "Yes" << endl;

  return 0;
}

Compiler output:
a.cc: In instantiation of 'SegmentTree<T>::SegmentTree(int, const T&, const Op&) [with Op = main()::<lambda(const int&, const int&)>; T = std::pair<int, long unsigned int>]':
a.cc:112:78:   required from here
  112 |                      [](const int& lh, const int& rh) { return max(lh, rh); });
      |                                                                              ^
a.cc:29:25: error: no matching function for call to 'std::function<std::pair<int, long unsigned int>(const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&)>::function(const main()::<lambda(const int&, const int&)>&)'
   29 |       : unit(identity), op(operation), container(msb(size * 2 - 1)) {
      |                         ^~~~~~~~~~~~~
In file included from /usr/include/c++/14/functional:59,
                 from a.cc:3:
/usr/include/c++/14/bits/std_function.h:435:9: note: candidate: 'template<class _Functor, class _Constraints> std::function<_Res(_ArgTypes ...)>::function(_Functor&&) [with _Constraints = _Functor; _Res = std::pair<int, long unsigned int>; _ArgTypes = {const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&}]'
  435 |         function(_Functor&& __f)
      |         ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:435:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/type_traits: In substitution of 'template<bool _Cond, class _Tp> using std::__enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = void]':
/usr/include/c++/14/bits/std_function.h:353:8:   required by substitution of 'template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using std::function<_Res(_ArgTypes ...)>::_Requires = std::__enable_if_t<((bool)_Cond::value), _Tp> [with _Cond = std::function<std::pair<int, long unsigned int>(const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&)>::_Callable<const main()::<lambda(const int&, const int&)>&, main()::<lambda(const int&, const int&)>, std::__invoke_result<main()::<lambda(const int&, const int&)>&, const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&> >; _Tp = void; _Res = std::pair<int, long unsigned int>; _ArgTypes = {const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&}]'
  353 |         using _Requires = __enable_if_t<_Cond::value, _Tp>;
      |               ^~~~~~~~~
/usr/include/c++/14/bits/std_function.h:434:9:   required from 'SegmentTree<T>::SegmentTree(int, const T&, const Op&) [with Op = main()::<lambda(const int&, const int&)>; T = std::pair<int, long unsigned int>]'
  434 |                typename _Constraints = _Requires<_Callable<_Functor>>>
      |                ^~~~~~~~
a.cc:112:78:   required from here
  112 |                      [](const int& lh, const int& rh) { return max(lh, rh); });
      |                                                                              ^
/usr/include/c++/14/type_traits:138:11: error: no type named 'type' in 'struct std::enable_if<false, void>'
  138 |     using __enable_if_t = typename enable_if<_Cond, _Tp>::type;
      |           ^~~~~~~~~~~~~
a.cc: In instantiation of 'SegmentTree<T>::SegmentTree(int, const T&, const Op&) [with Op = main()::<lambda(const int&, const int&)>; T = std::pair<int, long unsigned int>]':
a.cc:112:78:   required from here
  112 |                      [](const int& lh, const int& rh) { return max(lh, rh); });
      |                                                                              ^
/usr/include/c++/14/bits/std_function.h:404:7: note: candidate: 'std::function<_Res(_ArgTypes ...)>::function(std::function<_Res(_ArgTypes ...)>&&) [with _Res = std::pair<int, long unsigned int>; _ArgTypes = {const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&}]'
  404 |       function(function&& __x) noexcept
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:404:27: note:   no known conversion for argument 1 from 'const main()::<lambda(const int&, const int&)>' to 'std::function<std::pair<int, long unsigned int>(const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&)>&&'
  404 |       function(function&& __x) noexcept
      |                ~~~~~~~~~~~^~~
/usr/include/c++/14/bits/std_function.h:386:7: note: candidate: 'std::function<_Res(_ArgTypes ...)>::function(const std::function<_Res(_ArgTypes ...)>&) [with _Res = std::pair<int, long unsigned int>; _ArgTypes = {const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&}]'
  386 |       function(const function& __x)
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:386:32: note:   no known conversion for argument 1 from 'const main()::<lambda(const int&, const int&)>' to 'const std::function<std::pair<int, long unsigned int>(const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&)>&'
  386 |       function(const function& __x)
      |                ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/std_function.h:375:7: note: candidate: 'std::function<_Res(_ArgTypes ...)>::function(std::nullptr_t) [with _Res = std::pair<int, long unsigned int>; _ArgTypes = {const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&}; std::nullptr_t = std::nullptr_t]'
  375 |       function(nullptr_t) noexcept
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:375:16: note:   no known conversion for argument 1 from 'const main()::<lambda(const int&, const int&)>' to 'std::nullptr_t'
  375 |       function(nullptr_t) noexcept
      |                ^~~~~~~~~
/usr/include/c++/14/bits/std_function.h:368:7: note: candidate: 'std::function<_Res(_ArgTypes ...)>::function() [with _Res = std::pair<int, long unsigned int>; _ArgTypes = {const std::pair<int, long unsigned int>&, const std::pair<int, long unsigned int>&}]'
  368 |       function() noexcept
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:368:7: note:   candidate expects 0 arguments, 1 provided