Language: C++

#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 1000010
using namespace std;
ll n;
vector<pair<ll,ll> > v;
char buf[N];
string s;
struct ST{
	ll val,l,r,ind;
}segt[4*N];
void build_tree(ll x,ll l,ll r)
{
	segt[x].l=l;
	segt[x].r=r;
	if(l==r)
	{
		segt[x].ind=l;
		segt[x].val=v[l].S;
		return;
	}
	ll mid=(l+r)>>1,a=x<<1;
	if(mid>=l)
	{
		build_tree(a,l,mid);
	}
	if(mid<r)
	{
		build_tree(a+1,mid+1,r);
	}
	segt[x].val=max(segt[a].val,segt[a+1].val);
	if(segt[a].val<segt[a+1].val)
	{
		segt[x].ind=segt[a+1].ind;
	}
	else
	{
		segt[x].ind=segt[a].ind;
	}
	return;
}
void update(ll x,ll l)
{
	if(segt[x].l==l&&segt[x].r==l)
	{
		segt[x].val=-INF;
		segt[x].ind=-1;
		return;
	}
	ll mid=(segt[x].l+segt[x].r)>>1,a=x<<1;
	if(mid>=l)
	{
		update(a,l);
	}
	else
	{
		update(a+1,l);
	}
	segt[x].val=max(segt[a].val,segt[a+1].val);
	if(segt[a].val<segt[a+1].val)
	{
		segt[x].ind=segt[a+1].ind;
	}
	else
	{
		segt[x].ind=segt[a].ind;
	}
	return;
}
ll getans(ll x,ll l,ll r)
{
	ll tl=segt[x].l,tr=segt[x].r;
	if(l<=tl&&tr<=r)
	{
		return segt[x].ind;
	}
	else if(tl>r||tr<l)
	{
		return -1;
	}
	ll mid=(tl+tr)>>1,a=x<<1,ret1=-INF,ret2=-INF,x1=-1,x2=-1;
	if(mid>=l)
	{
		x1=getans(a,l,r);
		if(x1!=-1)
		{
			ret1=v[x1].S;
		}
	}
	if(mid<r)
	{
		x2=getans(a+1,l,r);
		if(x2!=-1)
		{
			ret2=v[x1].S;
		}
	}
	if(ret1<ret2)
	{
		return x2;
	}
	return x1;
}
int main(){
	ll i,j,pre,mn;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%s",&buf);
		s=buf;
		mn=INF;
		pre=0;
		for(j=0;j<s.size();j++)
		{
			if(s[j]=='(')
			{
				pre++;
			}
			else
			{
				pre--;
			}
			mn=min(mn,pre);
		}
		v.push_back(make_pair(mn,pre));
	}
	sort(v.begin(),v.end());
	build_tree(1,0,n-1);
	pre=0;
	for(i=0;i<n;i++)
	{
		mn=0-pre;
		j=lower_bound(v.begin(),v.end(),make_pair(mn,-INF))-v.begin();
		j=getans((ll)1,j,n-1);
		if(j==-1)
		{
			puts("No");
			return 0;
		}
		pre+=v[j].S;
		update(1,j); 
	}
	if(pre!=0)
	{
		puts("No");
	}
	else
	{
		puts("Yes");
	}
	return 0;
}

Compiler output:
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,
                 from a.cc:1:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<std::pair<long long int, long long int>*, std::vector<std::pair<long long int, long long int> > >; _Value = const std::pair<long long int, int>]':
/usr/include/c++/14/bits/stl_algobase.h:1504:14:   required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<pair<long long int, long long int>*, vector<pair<long long int, long long int> > >; _Tp = pair<long long int, int>; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
 1504 |           if (__comp(__middle, __val))
      |               ~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1539:32:   required from '_ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = __gnu_cxx::__normal_iterator<pair<long long int, long long int>*, vector<pair<long long int, long long int> > >; _Tp = pair<long long int, int>]'
 1539 |       return std::__lower_bound(__first, __last, __val,
      |              ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
 1540 |                                 __gnu_cxx::__ops::__iter_less_val());
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:142:16:   required from here
  142 |                 j=lower_bound(v.begin(),v.end(),make_pair(mn,-INF))-v.begin();
      |                   ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:69:22: error: no match for 'operator<' (operand types are 'std::pair<long long int, long long int>' and 'const std::pair<long long int, int>')
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:1241:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __normal_iterator<_IteratorL, _Container>&, const __normal_iterator<_IteratorR, _Container>&)'
 1241 |     operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1241:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1249:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __normal_iterator<_Iterator, _Container>&, const __normal_iterator<_Iterator, _Container>&)'
 1249 |     operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1249:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
In file included from /usr/include/c++/14/regex:68,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:181:
/usr/include/c++/14/bits/regex.h:1143:5: note: candidate: 'template<class _BiIter> bool std::__cxx11::operator<(const sub_match<_BiIter>&, const sub_match<_BiIter>&)'
 1143 |     operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1143:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/regex.h:1224:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator<(__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&, const sub_match<_BiIter>&)'
 1224 |     operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1224:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/regex.h:1317:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator<(const sub_match<_BiIter>&, __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&)'
 1317 |     operator<(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1317:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/regex.h:1391:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<(const typename std::iterator_traits<_Iter>::value_type*, const sub_match<_BiIter>&)'
 1391 |     operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1391:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'const std::pair<long long int, int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/regex.h:1485:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type*)'
 1485 |     operator<(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1485:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/regex.h:1560:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<(const typename std::iterator_traits<_Iter>::value_type&, const sub_match<_BiIter>&)'
 1560 |     operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1560:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'const std::pair<long long int, int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/regex.h:1660:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type&)'
 1660 |     operator<(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1660:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64:
/usr/include/c++/14/bits/stl_pair.h:1045:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator<(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1045 |     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1045:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   deduced conflicting types for parameter '_T2' ('long long int' and 'int')
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:448:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  448 |     operator<(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:448:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'const std::reverse_iterator<_Iterator>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  493 |     operator<(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:69:22: note:   'std::pair<long long int, long long int>' is not derived from 'const std::reverse_iterator<_Iterator>'
   69 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1694:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_Iterat