Problem:
Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<cstring>
#include<map>
 
 
using namespace std;
#define int long long int
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define pb push_back
//#define mod 1000000007
#define mod 998244353


int N;
string     S[1000005];
int  leftbra[1000005];
int rightbra[1000005];
int L[1000005];
int R[1000005];
vector<int> right0;
vector<int> left0;
vector<int> not0;
signed main(){
    scanf("%lld",&N);
    int countleft=0;
    int countright=0;
    rep(i,N){
        char sbuf[1000005];
        scanf("%s",sbuf);
        
        L[0]=0;
        rep(j,strlen(sbuf)){
            if(j>0)L[j]=L[j-1];
            if(sbuf[j]=='('){
                countleft++;
                L[j]+=1;
            }else{
                countright++;
                if(L[j]>0)L[j]-=1;
            }
        }
        leftbra[i]=L[strlen(sbuf)-1];
        
        R[strlen(sbuf)-1]=0;
        for(int j=strlen(sbuf)-1;j>=0;j--){
            if(j<strlen(sbuf)-1)R[j]=R[j+1];
            if(sbuf[j]==')'){
                R[j]+=1;
            }else{
                if(R[j]>0)R[j]-=1;
            }
        }
        rightbra[i]=R[0];
        if(rightbra[i]==0&&leftbra[i]==0)continue;
        if(rightbra[i]==0)right0.pb(i);
        if(leftbra[i]==0)left0.pb(i);
        if(rightbra[i]!=0&&leftbra[i]!=0)not0.pb(i);
    }
    int ans=0;
    if(countleft!=countright){printf("No\n");return 0;}
    int leftbra_nokori=0;
    int rightbra_nokori=0;
    rep(i,right0.size()){
        leftbra_nokori+=leftbra[right0[i]];
    }
    rep(i,left0.size()){
        rightbra_nokori+=rightbra[left0[i]];
    }
    int count=0;
    while(count<not0.size()){
        rep(i,not0.size()){
            if(rightbra[not0[i]]<=leftbra_nokori){
                leftbra_nokori-=rightbra[not0[i]];
                leftbra_nokori+=leftbra[not0[i]];
                rightbra[not0[i]]=LLINF;
                continue;
            }else if(i==not0.size()-1){
                printf("No\n");return 0;
            }
        }
    }
    printf("Yes\n");
    return 0;
}