Language: C++

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

void solve(std::istream &in, std::ostream &out);

int main() {
#ifdef LOCAL
  freopen("../IO/f.in", "r", stdin);
//  freopen("../IO/f.out", "w", stdout);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
#endif
  solve(std::cin, std::cout);
  return 0;
}

constexpr int N = 1000'0225;
char buff[N];

void solve(std::istream &in, std::ostream &out) {
  int n;
  in >> n;
  vector<pair<int, int>> sq;
  for (int i = 0; i < n; ++i) {
    in >> buff;
    int len = strlen(buff);
    int mn = len, bal = 0;
    for (int j = 0; j < len; ++j) {
      if (buff[j] == '(') {
        ++bal;
      } else {
        --bal;
      }
      mn = min(mn, bal);
    }
    if (mn == 0 && bal == 0) continue;
    if (mn < 0) {
      sq.emplace_back(-mn, bal - mn);
    } else {
      sq.emplace_back(0, bal);
    }
  }
  if (sq.empty()) {
    out << "Yes\n";
    return;
  }
  int64_t suml = 0, sumr = 0;
  for (auto pr : sq) {
    suml += pr.first, sumr += pr.second;
  }
  if (suml != sumr) {
    cout << "No\n";
    return;
  }
  sort(sq.begin(), sq.end(), [&](auto a, auto b) {
    return tie(a.first, -a.second) < tie(b.first, -b.second);
  });
  sumr = 0, suml = 0;
  bool bad = false;
  for (auto pr : sq) {
    sumr -= pr.first;
    bad |= sumr < 0;
    sumr += pr.second;
  }
  if (bad || sumr != 0) {
    out << "No\n";
  } else {
    out << "Yes\n";
  }
}


Compiler output:
a.cc: In instantiation of 'solve(std::istream&, std::ostream&)::<lambda(auto:28, auto:29)> [with auto:28 = std::pair<int, int>; auto:29 = std::pair<int, int>]':
/usr/include/c++/14/bits/predefined_ops.h:158:30:   required from 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >; _Compare = solve(std::istream&, std::ostream&)::<lambda(auto:28, auto:29)>]'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<int, int>*, vector<pair<int, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<solve(std::istream&, std::ostream&)::<lambda(auto:28, auto:29)> >]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<int, int>*, vector<pair<int, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<solve(std::istream&, std::ostream&)::<lambda(auto:28, auto:29)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<pair<int, int>*, vector<pair<int, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<solve(std::istream&, std::ostream&)::<lambda(auto:28, auto:29)> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<pair<int, int>*, vector<pair<int, int> > >; _Compare = solve(std::istream&, std::ostream&)::<lambda(auto:28, auto:29)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:57:7:   required from here
   57 |   sort(sq.begin(), sq.end(), [&](auto a, auto b) {
      |   ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   58 |     return tie(a.first, -a.second) < tie(b.first, -b.second);
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   59 |   });
      |   ~~   
a.cc:58:15: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
   58 |     return tie(a.first, -a.second) < tie(b.first, -b.second);
      |            ~~~^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/tuple:2791:19: note:   initializing argument 2 of 'constexpr std::tuple<_Elements& ...> std::tie(_Elements& ...) [with _Elements = {int, int}]'
 2791 |     tie(_Elements&... __args) noexcept
      |         ~~~~~~~~~~^~~~~~~~~~
a.cc:58:41: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
   58 |     return tie(a.first, -a.second) < tie(b.first, -b.second);
      |                                      ~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2791:19: note:   initializing argument 2 of 'constexpr std::tuple<_Elements& ...> std::tie(_Elements& ...) [with _Elements = {int, int}]'
 2791 |     tie(_Elements&... __args) noexcept
      |         ~~~~~~~~~~^~~~~~~~~~