Language: C++

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

using ll = long long int;
typedef long double ld;
typedef pair<ll,ll> pll;

#define sz(v) (ll)v.size()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define gcd(a,b) __gcd(a,b)
#define all(v) v.begin(),v.end()

struct comp{
	bool operator()(pair<string,ll> a,pair<string,ll> b){
		return a.s>=b.s;
	}
};
bool check(string s)
{
	ll t=0;
	for(auto it:s)
	{
		if(it=='(')t++;
		else t--;
		if(t<0) return 0;
	}
	return t==0;
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n;
	string s,p="",ne="",z="";
	cin>>n;
	multiset<pair<string,ll>,comp> vp,vne,vz;
	for(ll i=0;i<n;i++)
	{
		cin>>s;
		ll temp=0;
		for(auto it:s)
			if(it=='(')temp++;
			else temp--;
		if(temp>0)vp.insert(mp(s,temp));
		else if(temp<0)vne.insert(mp(s,temp));
		else vz.insert(mp(s,temp));
	}
	s="";
	for(auto it:vp)s+=it.f;
	for(auto it:vz)s+=it.f;
	for(auto it:vne)s+=it.f;
	cout<<(check(s)?"Yes":"No")<<"\n";
	return 0;
}


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<std::__cxx11::basic_string<char>, long long int>; _Val = std::pair<std::__cxx11::basic_string<char>, long long int>; _KeyOfValue = std::_Identity<std::pair<std::__cxx11::basic_string<char>, long long int> >; _Compare = comp; _Alloc = std::allocator<std::pair<std::__cxx11::basic_string<char>, long long int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<std::__cxx11::basic_string<char>, long long int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2147:44:   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_equal_pos(const key_type&) [with _Key = std::pair<std::__cxx11::basic_string<char>, long long int>; _Val = std::pair<std::__cxx11::basic_string<char>, long long int>; _KeyOfValue = std::_Identity<std::pair<std::__cxx11::basic_string<char>, long long int> >; _Compare = comp; _Alloc = std::allocator<std::pair<std::__cxx11::basic_string<char>, long long int> >; key_type = std::pair<std::__cxx11::basic_string<char>, long long int>]'
 2147 |           __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
      |                                             ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2196:4:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(_Arg&&) [with _Arg = std::pair<std::__cxx11::basic_string<char>, long long int>; _Key = std::pair<std::__cxx11::basic_string<char>, long long int>; _Val = std::pair<std::__cxx11::basic_string<char>, long long int>; _KeyOfValue = std::_Identity<std::pair<std::__cxx11::basic_string<char>, long long int> >; _Compare = comp; _Alloc = std::allocator<std::pair<std::__cxx11::basic_string<char>, long long int> >; iterator = std::_Rb_tree<std::pair<std::__cxx11::basic_string<char>, long long int>, std::pair<std::__cxx11::basic_string<char>, long long int>, std::_Identity<std::pair<std::__cxx11::basic_string<char>, long long int> >, comp, std::allocator<std::pair<std::__cxx11::basic_string<char>, long long int> > >::iterator]'
 2196 |         = _M_get_insert_equal_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:510:36:   required from 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = std::pair<std::__cxx11::basic_string<char>, long long int>; _Compare = comp; _Alloc = std::allocator<std::pair<std::__cxx11::basic_string<char>, long long int> >; iterator = std::_Rb_tree<std::pair<std::__cxx11::basic_string<char>, long long int>, std::pair<std::__cxx11::basic_string<char>, long long int>, std::_Identity<std::pair<std::__cxx11::basic_string<char>, long long int> >, comp, std::allocator<std::pair<std::__cxx11::basic_string<char>, long long int> > >::const_iterator; value_type = std::pair<std::__cxx11::basic_string<char>, long long int>]'
  510 |       { return _M_t._M_insert_equal(std::move(__x)); }
      |                ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:48:22:   required from here
   48 |                 if(temp>0)vp.insert(mp(s,temp));
      |                           ~~~~~~~~~^~~~~~~~~~~~
/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 comp&, const std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long long int>&, const std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long long int>&>' evaluates to false