Language: C++

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

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define base 402653189
#define mod 998244353
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define MX 1000005

int n;
string str[MX];
struct data{
	int first;
	int second;
}pr[MX];

int tree[MX * 4];
stack<char>st;
int nn = 0;
int open = 0;
int close = 0;

void process(int pos){
	int siz = str[pos].size();
	for (int i = 0; i<siz; i++){
		if (str[pos][i] == '(') st.push('(');
		else if (str[pos][i] == ')' && !st.empty() && st.top() == '(') st.pop();
		else if (str[pos][i] == ')') st.push(')');
	}
	++nn;
	pr[nn].first = 0;
	pr[nn].second = 0;
	while (!st.empty()){
		if (st.top() == '(') pr[nn].first++;
		else pr[nn].second++;
		st.pop();
	}
	if (pr[nn].first && pr[nn].second) pr[nn].first -= pr[nn].second;
	else{
		open += pr[nn].first;
		close += pr[nn].second;
		--nn;
	}
}
bool cmp(const data &tmp, const data &tpp){
	if (tmp.second == tpp.second) return (tmp.first >= tpp.first);
	return (tmp.second<tpp.second);
}
void init(int node, int b, int e){
	if (b == e) {
		tree[node] = b;
		return;
	}
	int lef = node * 2, ri = (node * 2) + 1, mid = (b + e) / 2;
	init(lef, b, mid);
	init(ri, mid + 1, e);
	int lval = pr[tree[lef]].first;
	int rval = pr[tree[ri]].first;
	if (lval >= rval) tree[node] = tree[lef];
	else tree[node] = tree[ri];
}
void update(int node, int b, int e, int i, int pos){
	if (i>e || i<b) return;
	if (b >= i && e <= i) {
		tree[node] = pos;
		return;
	}
	int lef = node * 2, ri = (node * 2) + 1, mid = (b + e) / 2;
	update(lef, b, mid, i, pos);
	update(ri, mid + 1, e, i, pos);
	int lval = pr[tree[lef]].first;
	int rval = pr[tree[ri]].first;
	if (lval >= rval) tree[node] = tree[lef];
	else tree[node] = tree[ri];
}
int query(int node, int b, int e, int i, int j){
	if (i>j || i>e || j<b) return 0;
	if (b >= i && e <= j) return tree[node];
	int lef = node * 2, ri = (node * 2) + 1, mid = (b + e) / 2;
	int lPos = query(lef, b, mid, i, j);
	int rPos = query(ri, mid + 1, e, i, j);
	if (pr[lPos].first >= pr[rPos].first) return lPos;
	else return rPos;
}
int range(int value){
	int lo = 1, hi = nn, mid, tmp = -1;
	while (lo <= hi){
		mid = (lo + hi) / 2;
		if (pr[mid].second <= value){
			tmp = mid;
			lo = mid + 1;
		}
		else hi = mid - 1;
	}
	return tmp;
}

int main(){
	optimize();
	//freopen("input.txt","r",stdin);
	//freopen("output2.txt","w",stdout);

	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> str[i];
	}
	for (int i = 1; i <= n; i++) process(i);
	sort(pr + 1, pr + nn + 1, cmp);

	pr[0].first = -1e9;
	pr[0].second = 0;

	if (nn) init(1, 1, nn);

	int cnt = 0;
	bool flag = true;
	while (cnt<nn){
		int nPos = range(open);

		if (nPos == -1){
			flag = false;
			break;
		}
		int got = query(1, 1, nn, 1, nPos);

		if (got == 0) {
			flag = false;
			break;
		}
		open += pr[got].first;

		if (open<0){
			flag = false;
			break;
		}
		update(1, 1, nn, got, 0);
		++cnt;
	}
	if (cnt != nn || open != close) flag = false;

	if (flag) cout << "Yes" << endl;
	else cout << "No" << endl;

	return 0;
}


Compiler output:
a.cc:23:5: error: 'int close' redeclared as different kind of entity
   23 | int close = 0;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
                 from /usr/include/signal.h:328,
                 from /usr/include/c++/14/csignal:42,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:116,
                 from a.cc:2:
/usr/include/unistd.h:358:12: note: previous declaration 'int close(int)'
  358 | extern int close (int __fd);
      |            ^~~~~
a.cc: In function 'void process(int)':
a.cc:43:23: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   43 |                 close += pr[nn].second;
      |                 ~~~~~~^~~~~~~~~~~~~~~~
a.cc:43:23: error: assignment of read-only location 'close'
a.cc: At global scope:
a.cc:47:16: error: reference to 'data' is ambiguous
   47 | bool cmp(const data &tmp, const data &tpp){
      |                ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/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:14:8: note:                 'struct data'
   14 | struct data{
      |        ^~~~
a.cc:47:33: error: reference to 'data' is ambiguous
   47 | bool cmp(const data &tmp, const data &tpp){
      |                                 ^~~~
/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:14:8: note:                 'struct data'
   14 | struct data{
      |        ^~~~
a.cc: In function 'bool cmp(const int&, const int&)':
a.cc:48:17: error: request for member 'second' in 'tmp', which is of non-class type 'const int'
   48 |         if (tmp.second == tpp.second) return (tmp.first >= tpp.first);
      |                 ^~~~~~
a.cc:48:31: error: request for member 'second' in 'tpp', which is of non-class type 'const int'
   48 |         if (tmp.second == tpp.second) return (tmp.first >= tpp.first);
      |                               ^~~~~~
a.cc:48:51: error: request for member 'first' in 'tmp', which is of non-class type 'const int'
   48 |         if (tmp.second == tpp.second) return (tmp.first >= tpp.first);
      |                                                   ^~~~~
a.cc:48:64: error: request for member 'first' in 'tpp', which is of non-class type 'const int'
   48 |         if (tmp.second == tpp.second) return (tmp.first >= tpp.first);
      |                                                                ^~~~~
a.cc:49:21: error: request for member 'second' in 'tmp', which is of non-class type 'const int'
   49 |         return (tmp.second<tpp.second);
      |                     ^~~~~~
a.cc:49:32: error: request for member 'second' in 'tpp', which is of non-class type 'const int'
   49 |         return (tmp.second<tpp.second);
      |                                ^~~~~~
a.cc: In function 'int main()':
a.cc:141:31: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
  141 |         if (cnt != nn || open != close) flag = false;
      |                          ~~~~~^~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = data*; _Iterator2 = data*; _Compare = bool (*)(const int&, const int&)]':
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int&, const int&)>]'
 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 = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int&, const int&)>]'
 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 = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int&, const int&)>]'
 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 = data*; _Compare = bool (*)(const int&, const int&)]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:110:6:   required from here
  110 |         sort(pr + 1, pr + nn + 1, cmp);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:158:30: error: invalid initialization of reference of type 'const int&' from expression of type 'data'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Value = data; _Iterator = data*; _Compare = bool (*)(const int&, const int&)]':
/usr/include/c++/14/bits/stl_algo.h:1757:20:   required from 'void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = data*; _Compare = __gnu_cxx::__ops::_Val_comp_iter<bool (*)(const int&, const int&)>]'
 1757 |       while (__comp(__val, __next))
      |              ~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1785:36:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int&, const int&)>]'
 1785 |             std::__unguarded_linear_insert(__i,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
 1786 |                                 __gnu_cxx::__ops::__val_comp_iter(__comp));
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int&, const int&)>]'
 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 = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int&, const int&)>]'
 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 = data*; _Compare = bool (*)(const int&, const int&)]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:110:6:   required from here
  110 |         sort(pr + 1, pr + nn + 1, cmp);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:240:30: error: invalid initialization of reference of type 'const int&' from expression of type 'data'
  240 |         { return bool(_M_comp(__val, *__it)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = data*; _Value = data; _Compare = bool (*)(const int&, const int&)]':
/usr/include/c++/14/bits/stl_heap.h:140:48:   required from