Example

Input

3
()(()((
))()()(()
)())(())


Output

Yes
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <tuple>
#include <algorithm>
using namespace std;

const int INF = 1010;
pair<int, int> calc(string s) {
    // 全体の合計、負になった時の絶対値最大
    int A = 0, B = 0;
    for(size_t i=0; i<s.length(); i++) {
        if(s[i] == '(') A++;
        if(s[i] == ')') A--;

        if(A < 0) B = max(B, -A);
    }
    return make_pair(A, B);
}

bool solve(vector< pair<int, int> > vals) {
    sort(vals.begin(), vals.end(), [&](pair<int, int> L, pair<int, int> R) {
        if(L.second != R.second) return L.second < R.second;
        return L.first < R.first;
    });

    int pre = 0, N = vals.size();
    vector<int> checked(N);
    for(int z=0; z<N; z++) {
        int ma = -INF, idx = -1;
        for(int i=0; i<N; i++) {
            if(checked[i]) continue;
            if(pre < vals[i].second) break;

            if(ma < vals[i].first) {
                ma = vals[i].first;
                idx = i;
            }
        }

        if(idx < 0) return false;
        checked[idx] = true;
        pre += vals[idx].first;
        // printf("take: A = %d, B = %d\n", vals[idx].first, vals[idx].second);
    }

    return true;
}

int main() {
    int N; cin >> N;
    vector< pair<int, int> > posi, nega;

    int sum = 0;
    for(int i=0; i<N; i++) {
        string s; cin >> s;
        
        pair<int, int> tmp = calc(s);
        if(tmp.first >= 0) {
            posi.push_back(tmp);
        }
        else {
            int A = tmp.first, B = tmp.second;
            A = abs(A), B = B - A;
            nega.push_back(make_pair(A, B));
        }
        sum += tmp.first;
    }

    if(sum != 0) {
        cout << "No" << endl;
    }
    else {
        bool ans = solve(posi) && solve(nega);
        cout << (ans ? "Yes" : "No") << endl;
    }
    return 0;
}
