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 <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
//#include <math.h>
#include <string>
#include <numeric>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep1(i,n) for(ll i=1;i<n;++i)
#define mrep(i,n) for(ll i=n;i>=0;--i)
#define all(a) (a).begin(),(a).end()
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool> >
#define pl pair<ll,ll>
#define inf 1001001001001001000
//#define mod 1000000007
#define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT{
    __INIT(){
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout<<fixed<<setprecision(15);
    }
}__init;

int main(void){
    ll n;
    cin>>n;
    vector<pl> pitsu(n);
    bool nisyu = false;
    ll left = 0;
    ll right = 0;
    ll dleft = 0;
    ll dright = 0;
    rep(i,n){
        string s;
        cin>>s;
        ll l=0,r=0;
        rep(j,s.size()){
            if(s[j] == '('){
                l++;
            }
            if(s[j] == ')'){
                if(l > 0) l--;
                else r++;
            }
        }
        if(l == 0 && r == 0) continue;
        if((l == 0) || (r == 0)){
            nisyu = true;
            if(r == 0){
                if(dright > 0){
                    dright -= l;
                    l = 0;
                    if(dright <= 0){
                        l -= dright;
                        dright = 0;
                        left += dleft;
                        dleft = 0;
                    }
                }
                left += l;
            }
            if(l == 0){
                if(dleft > 0){
                    dleft -= r;
                    r = 0;
                    if(dleft <= 0){
                        r -= dleft;
                        dleft = 0;
                        right += dright;
                        dright = 0;
                    }
                }
                right += r;
            }
            continue;
        }
        else{
            if(dleft == 0){
                dright = r;
                dleft = l;
                if(left > 0){
                    dright -= left;
                    left = 0;
                    if(dright<0) {
                        left -= dright;
                        dright = 0;
                        left += dleft;
                        dleft = 0;
                    }
                }
                if(right > 0){
                    dleft -= right;
                    right = 0;
                    if(dleft<0) {
                        right -= dleft;
                        dleft = 0;
                        right += dright;
                        dright = 0;
                    }
                }
            }
            else{
                if(dright > dleft){
                    dright -= l;
                    l = 0;
                    if(dright < 0){
                        dleft -= dright;
                        dright = 0;
                    }
                    dright += r;
                }
                else if(dright < dleft){
                    dleft -= r;
                    r = 0;
                    if(dleft < 0){
                        dright -= dleft;
                        dleft = 0;
                    }
                    dleft += l;
                }
            }
        }
    }
    if(dleft == 0 && dright == 0 && left == right){
        cout<<"Yes"<<endl;
    }
    else{
        cout<<"No"<<endl;
    }
}