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)

#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

typedef long long lli;
typedef pair<int, int> pii;

const int maxn = 5e6 + 5;
const int inf = 1e9 + 7;

int n;
int len[maxn];
string s[maxn];
pii f[maxn];

vector<pii> v;

struct segment_tree
{
    vector<pii> nodes;

    void init(int n)
    {
        nodes.resize((n + 5) * 4);
    }

    pii combine(const pii&a, const pii&b)
    {
        if(a.fi >= b.fi) return a;
        return b;
    }

    void build(int x, int l, int r)
    {
        if(l == r)
        {
            nodes[x] = pii(v[l].se, l);
            return;
        }
        int mid = (l + r) / 2;
        build(x * 2, l, mid);
        build(x * 2 + 1, mid + 1, r);
        nodes[x] = combine(nodes[x * 2], nodes[x * 2 + 1]);
    }

    void update(int x, int l, int r, int p, int k)
    {
        if(p < l || p > r) return;
        if(l == r)
        {
            nodes[x] = pii(k, l);
            return;
        }
        int mid = (l + r) / 2;
        update(x * 2, l, mid, p, k);
        update(x * 2 + 1, mid + 1, r, p, k);
        nodes[x] = combine(nodes[x * 2], nodes[x * 2 + 1]);
    }

    pii get(int x, int l, int r, int L, int R)
    {
        if(L > r || l > R) return pii(-inf, -1);
        if(l >= L && r <= R)
            return nodes[x];
        int mid = (l + r) / 2;
        return combine(get(x * 2, l, mid, L, R),
                       get(x * 2 + 1, mid + 1, r, L, R));
    }
} tree;

void read_input()
{
    cin >> n;
    for(int i = 1; i <= n; ++i)
    {
        cin >> s[i];
        len[i] = sz(s[i]);
        s[i] = " " + s[i];
    }
}

void init()
{
    for(int i = 1; i <= n; ++i)
    {
        f[i] = pii(0, 0);
        for(int j = 1; j <= len[i]; ++j)
        {
            if(s[i][j] == '(')
                ++f[i].fi;
            else
            {
                if(f[i].fi == 0)
                    ++f[i].se;
                else --f[i].fi;
            }
        }
    }
    for(int i = 1; i <= n; ++i)
        v.push_back(pii(f[i].se, f[i].fi - f[i].se));
    sort(v.begin(), v.end());
    tree.init(maxn);
    tree.build(1, 0, n - 1);
}

void solve()
{
    int cur = 0, rem = n;
    while(rem > 0)
    {
        int p = lower_bound(v.begin(), v.end(), pii(cur, inf)) - v.begin() - 1;
        if(p == -1)
        {
            cout << "No\n";
            return;
        }
        pii tmp = tree.get(1, 0, n - 1, 0, p);
        if(cur + tmp.fi < 0)
        {
            cout << "No\n";
            return;
        }
        cur += tmp.fi;
        tree.update(1, 0, n - 1, tmp.se, -inf);
        --rem;
    }
    cout << (cur == 0 ? "Yes\n" : "No\n");
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    read_input();
    init();
    solve();
}