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;

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }


const string YES = "Yes";
const string NO = "No";

void func(long long N, std::vector<std::string> S){

    //):l, (:r
    pair<lli,lli> p;
    p.first=0;
    p.second=0;

    vector<pair<lli,lli>> v;

    REP(i,0,N)
    {
        lli left = 0;
        lli right = 0;

        REP(j,0,S[i].size()){
            if(S[i][j] == '('){
                left++;
            }
            else{
                if(left>0)left--;
                else right++;
            }
        }
        if(left==0 && right==0){
            continue;
        }
        v.push_back({left,right});
        /*
        LOG4(p.first,p.second,right,left);
        lli a = min(p.first,right);
        p.first -= a;
        right -= a;
        lli b = min(p.second,left);
        p.second -= b;
        left -= b;
        p.first += left;
        p.second += right;
        */
    }

    if(v.size()==0){
        cout<<YES<<endl;
        return;
    }

    lli lSum = 0,rSum=0;

    REP(i,0,v.size()){
        lSum += v[i].first;
        rSum += v[i].second;
    }

    REP(i,0,v.size()){
        lli nowLSum = lSum -v[i].first;
        lli nowRSum = rSum -v[i].second;

        if(nowLSum < v[i].second || nowRSum < v[i].first){
            continue;
        }

        if(nowLSum - v[i].second + nowRSum - v[i].first == 0){
            cout<<YES<<endl;
            return;
        }
    }

    cout<<NO<<endl;

}

int main(){
    // cout << fixed << setprecision(5);

    long long N;
    scanf("%lld",&N);
    std::vector<std::string> S(N);
    for(int i = 0 ; i < N ; i++){
        std::cin >> S[i];
    }
    func(N, std::move(S));
    return 0;
}