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 pair<int, int>&tmp, const pair<int, int>&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: 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 std::pair<int, int>&, const std::pair<int, 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 pair<int, int>&, const pair<int, 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 pair<int, int>&, const pair<int, 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 pair<int, int>&, const pair<int, 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 pair<int, int>&, const pair<int, 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 std::pair<int, 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 std::pair<int, int>&, const std::pair<int, 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 pair<int, int>&, const pair<int, 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 pair<int, int>&, const pair<int, 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 pair<int, int>&, const pair<int, 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 pair<int, int>&, const pair<int, 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 pair<int, int>&, const pair<int, 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 std::pair<int, 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 std::pair<int, int>&, const std::pair<int, int>&)]':
/usr/include/c++/14/bits/stl_heap.h:140:48:   required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = data*; _Distance = long int; _Tp = data; _Compare = __gnu_cxx::__ops::_Iter_comp_val<bool (*)(const pair<int, int>&, const pair<int, int>&)>]'
  140 |       while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
      |                                          ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:247:23:   required from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = data*; _Distance = long int; _Tp = data; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const pair<int, int>&, const pair<int, int>&)>]'
  247 |       std::__push_heap(__first, __holeIndex, __topIndex,
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  248 |                        _GLIBCXX_MOVE(__value), __cmp);
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:356:22:   required from 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const pair<int, int>&, const pair<int, int>&)>]'
  356 |           std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
      |           ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  357 |                              __comp);
      |                              ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1593:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const pair<int, int>&, const pair<int, int>&)>]'
 1593 |       std::__make_heap(__first, __middle, __comp);
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1868:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const pair<int, int>&, const pair<int, int>&)>]'
 1868 |       std::__heap_select(__first, __middle, __last, __comp);
      |       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1884:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = data*; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const pair<int, int>&, const pair<int, int>&)>]'
 1884 |               std::__partial_sort(__first, __last, __last, __comp);
      |               ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1905:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = data*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const pair<int, int>&, const pair<int, int>&)>]'
 1905 |           std::__introsort_loop(__first, __last,
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1907 |                                 __comp);
      |                                 ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h: