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<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<limits.h>
#define MOD 1000000007

int *array;

int min(int a, int b) {
    return a<b?a:b;
}

int max(int a, int b) {
    return a>b?a:b;
}

int compare(const void* a, const void* b) {
    return *(int*)a-*(int*)b;
}

int cmp(const void *a, const void *b){
    int ia = *(int *)a;
    int ib = *(int *)b;
    return array[ia] < array[ib] ? -1 : array[ia] > array[ib];
}

int abs(int n) {
    if(n < 0)
        return n*(-1);
    return n;
}

void swap(int *a, int *b) {
    *a = (*a)^(*b);
    *b = (*a)^(*b);
    *a = (*a)^(*b);
}

int binSearch(int a[], int x, int l, int r) {
    if(r >= l) {
        int mid = l + ((r-l)>>1);
        if(a[mid] == x)
            return mid;
        if(a[mid] > x) {
            return binSearch(a, x, l, mid-1);
        }
        else if(a[mid] < x) {
            return binSearch(a, x, mid+1, r);
        }
    }
    return -1;
}

void testCase() {
    int i, n;
    if(scanf("%d", &n)) {
        // char s[1000001];
        int open=0, close=0;
        for(i=0;i<n;i++) {
            int x=0, y=0;
            char s[1000001];
            if(scanf("%s", s));
            int j=0;
            for(int k=0;s[k];k++) {
                if(s[k] == '(') {
                    j++;
                }
                else if(s[k] == ')') {
                    if(j > 0) {
                        j--;
                    }
                    else {
                        x++;
                    }
                }
            }
            y += j;
            if(i == 0) {
                open = y;
                close = x;
            }
            else {
                if(x == 0 && y != 0) {
                    close -= y;
                    if(close < 0) {
                        close=0;
                        open += (y-close);
                    }
                }
                else if(x != 0 && y == 0) {
                    open -= x;
                    if(open < 0) {
                        open=0;
                        close += (y-open);
                    }
                }
                else {
                    open += x;
                    close += y;
                }
            }
        }
        if(open == 0 && close == 0) {
            printf("Yes");
        }
        else
            printf("No");
    }
}

int main() {
    int t=1;
    // scanf("%d", &t);
    while(t--) {
        testCase();
        printf("\n");
    }
    return 0;
}