Language: C++

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

using ll = long long;
using ull = unsigned long long;
#define repi(n) for (long long i = 0; i < (n); ++i)
#define repj(n) for (long long j = 0; j < (n); ++j)
#define repk(n) for (long long k = 0; k < (n); ++k)
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define itr(itr) for (auto&& v : (itr))
#define updatemax(t, v) (t = std::max(t, (v)))
#define updatemin(t, v) (t = std::min(t, (v)))

struct Compare {
  bool operator()(const pair<ll, ll>& lhs, const pair<ll, ll>& rhs) {
    return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second > rhs.second);
  }
};

string s[1000010];
ll mins[1000010], lvs[1000010];
int main() {
  ll n;
  cin >> n;
  repi(n) cin >> s[i];
  ll lvsum = 0;
  repi(n) {
    ll l = s[i].length();
    repj(l) {
      lvs[i] += (s[i][j] == '(') * 2 - 1;
      updatemin(mins[i], lvs[i]);
    }
    lvsum += lvs[i];
  }
  if (lvsum != 0) {
    cout << "No" << endl;
    return 0;
  }
  set<pair<ll, ll>, Compare> ps;
  repi(n) ps.emplace(mins[i], lvs[i]);
  bool re = true;
  ll lv = 0;
  while (!ps.empty()) {
    auto x = ps.lower_bound(pair<ll, ll>(-lv, LLONG_MIN));
    if (x == ps.end()) {
      re = false;
      break;
    }
    ll pmin = x->first, pval = x->second;
    ps.erase(x);
    lv += pval;
  }
  cout << (re ? "Yes" : "No") << endl;
}


Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = Compare; _Alloc = std::allocator<std::pair<long long int, long long int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<long long int, long long int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::pair<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = Compare; _Alloc = std::allocator<std::pair<long long int, long long int> >; key_type = std::pair<long long int, long long int>]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2433:15:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_unique(_Args&& ...) [with _Args = {long long int&, long long int&}; _Key = std::pair<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = Compare; _Alloc = std::allocator<std::pair<long long int, long long int> >]'
 2433 |         auto __res = _M_get_insert_unique_pos(__z._M_key());
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:465:33:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::emplace(_Args&& ...) [with _Args = {long long int&, long long int&}; _Key = std::pair<long long int, long long int>; _Compare = Compare; _Alloc = std::allocator<std::pair<long long int, long long int> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<long long int, long long int>, std::pair<long long int, long long int>, std::_Identity<std::pair<long long int, long long int> >, Compare, std::allocator<std::pair<long long int, long long int> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<long long int, long long int> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::rebind<std::pair<long long int, long long int> >; typename _Alloc::value_type = std::pair<long long int, long long int>]'
  465 |         { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
      |                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:40:21:   required from here
   40 |   repi(n) ps.emplace(mins[i], lvs[i]);
      |           ~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const Compare&, const std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&>' evaluates to false