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 <bits/stdc++.h>

using namespace std;
bool cmp(pair<int, int> x, pair<int, int> y)
{
    int i=(x.first-x.second);
    int j=((y.first-y.second));
    if((i>0)&&(j<=0))
    {
        return true;
    }
    else if((j>0)&&(i<=0))
        return false;
    else
    {
    if(x.second==y.second)
        return x.first>y.first;
    else
        return x.second<y.second;
    }
}
int main() {
    int n, i, j, left=0, right=0, lexist=0, rexist=0, nodap=0, imsi;
    pair<int, int> su[1000001];
    char s[1000003];
    stack<int> x;
    for(i=1;i<=1000000;i++)
    {
        su[i].first=0;
        su[i].second=0;
    }
    cin>>n;
    for(i=1;i<=n;i++)
    {
        scanf("%s", (s+1));
        for(j=1;j<=strlen(s+1);j++)
        {
            if(s[j]=='(')
            {

            x.push(1);
            }
            else
            {

            if(x.empty()||(x.top()==0))
            x.push(0);
            else
            x.pop();
            }
        }
        while(!x.empty())
        {
            if(x.top()==1)
            {
                left++;
                su[i].first++;
                x.pop();
            }
            else
            {
                right++;
                su[i].second++;
                x.pop();
            }
        }
        if((su[i].second==0)&&su[i].first)
        lexist=1;
        if((su[i].first==0)&&su[i].second)
        rexist=1;
    }
    if((left==0)&&(right==0))
    printf("Yes");
    else
    {
    if((left==right))
    {
    sort(su+1, su+(n+1), cmp);
    imsi=0;
    for(i=1;i<=n;i++)
    {
        imsi-=su[i].second;
        if(imsi<0)
        nodap=1;
        imsi+=su[i].first;
    }
    
    
    if(nodap)
    printf("No");
    else
    printf("Yes");
    }
    else
    printf("No");
    }
    return 0;
}