Language: C++

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct data {
    int min, res;
};

bool comp1(const data &lh, const data &rh) { return lh.min < rh.min; }

bool comp2(const data &lh, const data &rh) {
    if (lh.min != rh.min) return lh.min > rh.min;
    return lh.res > rh.res;
}

int main() {
    int n;
    cin >> n;
    vector<data> v(n);
    int sum = 0;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        int now = 0;
        int mi = 0;
        for (char &c : s) {
            c == '(' ? now++ : now--;
            mi = min(mi, now);
        }
        v[i] = {mi, now};
        sum += now;
    }

    if (sum != 0) {
        cout << "No" << endl;
        return 0;
    }

    sort(v.begin(), v.end(), comp1);
    int remain = 0;
    while (v.back().min >= 0) remain += v.back().res, v.pop_back();

    reverse(v.begin(), v.end());
    vector<data> after;
    for (auto &x:v) {
        if (x.res >= 0) {
            if (remain + x.min >= 0)remain += x.res;
            else {
                cout << "No" << endl;
                return 0;
            }
        } else {
            after.push_back(x);
        }
    }

    sort(after.begin(), after.end(), comp2);
    for (auto &x:after) {
        if (remain + x.min >= 0)remain += x.res;
        else {
            cout << "No" << endl;
            return 0;
        }
    }

    cout << "Yes" << endl;
    return 0;
}

Compiler output:
a.cc:11:18: error: reference to 'data' is ambiguous
   11 | bool comp1(const data &lh, const data &rh) { return lh.min < rh.min; }
      |                  ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:7:8: note:                 'struct data'
    7 | struct data {
      |        ^~~~
a.cc:11:34: error: reference to 'data' is ambiguous
   11 | bool comp1(const data &lh, const data &rh) { return lh.min < rh.min; }
      |                                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:7:8: note:                 'struct data'
    7 | struct data {
      |        ^~~~
a.cc: In function 'bool comp1(const int&, const int&)':
a.cc:11:56: error: request for member 'min' in 'lh', which is of non-class type 'const int'
   11 | bool comp1(const data &lh, const data &rh) { return lh.min < rh.min; }
      |                                                        ^~~
a.cc:11:65: error: request for member 'min' in 'rh', which is of non-class type 'const int'
   11 | bool comp1(const data &lh, const data &rh) { return lh.min < rh.min; }
      |                                                                 ^~~
a.cc: At global scope:
a.cc:13:18: error: reference to 'data' is ambiguous
   13 | bool comp2(const data &lh, const data &rh) {
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:7:8: note:                 'struct data'
    7 | struct data {
      |        ^~~~
a.cc:13:34: error: reference to 'data' is ambiguous
   13 | bool comp2(const data &lh, const data &rh) {
      |                                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:7:8: note:                 'struct data'
    7 | struct data {
      |        ^~~~
a.cc: In function 'bool comp2(const int&, const int&)':
a.cc:14:12: error: request for member 'min' in 'lh', which is of non-class type 'const int'
   14 |     if (lh.min != rh.min) return lh.min > rh.min;
      |            ^~~
a.cc:14:22: error: request for member 'min' in 'rh', which is of non-class type 'const int'
   14 |     if (lh.min != rh.min) return lh.min > rh.min;
      |                      ^~~
a.cc:14:37: error: request for member 'min' in 'lh', which is of non-class type 'const int'
   14 |     if (lh.min != rh.min) return lh.min > rh.min;
      |                                     ^~~
a.cc:14:46: error: request for member 'min' in 'rh', which is of non-class type 'const int'
   14 |     if (lh.min != rh.min) return lh.min > rh.min;
      |                                              ^~~
a.cc:15:15: error: request for member 'res' in 'lh', which is of non-class type 'const int'
   15 |     return lh.res > rh.res;
      |               ^~~
a.cc:15:24: error: request for member 'res' in 'rh', which is of non-class type 'const int'
   15 |     return lh.res > rh.res;
      |                        ^~~
a.cc: In function 'int main()':
a.cc:21:16: error: template argument 1 is invalid
   21 |     vector<data> v(n);
      |                ^
a.cc:21:16: error: template argument 2 is invalid
a.cc:32:10: error: invalid types 'int[int]' for array subscript
   32 |         v[i] = {mi, now};
      |          ^
a.cc:41:12: error: request for member 'begin' in 'v', which is of non-class type 'int'
   41 |     sort(v.begin(), v.end(), comp1);
      |            ^~~~~
a.cc:41:23: error: request for member 'end' in 'v', which is of non-class type 'int'
   41 |     sort(v.begin(), v.end(), comp1);
      |                       ^~~
a.cc:43:14: error: request for member 'back' in 'v', which is of non-class type 'int'
   43 |     while (v.back().min >= 0) remain += v.back().res, v.pop_back();
      |              ^~~~
a.cc:43:43: error: request for member 'back' in 'v', which is of non-class type 'int'
   43 |     while (v.back().min >= 0) remain += v.back().res, v.pop_back();
      |                                           ^~~~
a.cc:43:57: error: request for member 'pop_back' in 'v', which is of non-class type 'int'
   43 |     while (v.back().min >= 0) remain += v.back().res, v.pop_back();
      |                                                         ^~~~~~~~
a.cc:45:15: error: request for member 'begin' in 'v', which is of non-class type 'int'
   45 |     reverse(v.begin(), v.end());
      |               ^~~~~
a.cc:45:26: error: request for member 'end' in 'v', which is of non-class type 'int'
   45 |     reverse(v.begin(), v.end());
      |                          ^~~
a.cc:46:16: error: template argument 1 is invalid
   46 |     vector<data> after;
      |                ^
a.cc:46:16: error: template argument 2 is invalid
a.cc:47:18: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   47 |     for (auto &x:v) {
      |                  ^
      |                  std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:47:18: error: 'end' was not declared in this scope; did you mean 'std::end'?
   47 |     for (auto &x:v) {
      |                  ^
      |                  std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:55:19: error: request for member 'push_back' in 'after', which is of non-class type 'int'
   55 |             after.push_back(x);
      |                   ^~~~~~~~~
a.cc:59:16: error: request for member 'begin' in 'after', which is of non-class type 'int'
   59 |     sort(after.begin(), after.end(), comp2);
      |                ^~~~~
a.cc:59:31: error: request for member 'end' in 'after', which is of non-class type 'int'
   59 |     sort(after.begin(), after.end(), comp2);
      |                               ^~~
a.cc:60:18: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   60 |     for (auto &x:after) {
      |                  ^~~~~
      |                  std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:60:18: error