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;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
typedef vector<vll> vvll;

#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define REP1(i,n) for(int i=1;i<((int)n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define yes cout<<"Yes"<<endl
#define YES cout<<"YES"<<endl
#define no cout<<"No"<<endl
#define NO cout<<"NO"<<endl
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin>>n;
  vector<string> v(n);
  REP(i,n)cin>>v[i];

  ll l=0,r=0;
  REP(i,n){
    REP(j,v[i].size()){
      if(v[i][j]=='(')
        l++;
      else
        r++;
    }
  }
  if(l!=r){
    no;
    return 0;
  }

  vpl before, after;
  REP(i,n){
    ll k=v[i].size();
    ll a=0,b=0;
    REP(j,k){
      if(v[i][j]==')'){
        if(b>0)
          b--;
        else
          a++;
      }
      else
        b++;
    }

    for(ll j=k-1; j>=0; j--){
      if(v[i][j]=='('){
        if(a>0)
          a--;
        else
          b++;
      }
      else
        a++;
    }

    if(b-a>0)
      before.push_back(pll(a,i));
    else
      after.push_back(pll(b,i));
  }


  sort(ALL(before));
  sort(ALL(after),greater<pll>());

  string ans="";
  for(auto b:before)
    ans+=v[b.SE];
  for(auto a:after)
    ans+=v[a.SE];

//  cout<<ans<<endl;
  if(ans[0]==')'||ans[ans.size()-1]=='(')
    cout<<"No"<<endl;
  else
    cout<<"Yes"<<endl;

}