Problem:
Score : 400 points

Problem Statement

You are given a string S of length N consisting of ( and ). Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence.

Here, a correct bracket sequence is defined as follows:

() is a correct bracket sequence.

If X is a correct bracket sequence, the concatenation of (, X and ) in this order is also a correct bracket sequence.

If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.

Every correct bracket sequence can be derived from the rules above.

Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.

Constraints

The length of S is N.

1 ≤ N ≤ 100

S consists of ( and ).

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S.

Sample Input 1

3
())

Sample Output 1

(())

Sample Input 2

6
)))())

Sample Output 2

(((()))())

Sample Input 3

8
))))((((

Sample Output 3

(((())))(((())))

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}

ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}

ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll nCr(ll n, ll r){
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}

//-----------------------ここから-----------

int main(void){
    int n;
    cin >> n;

    string s;
    cin >> s;

    int lcnt = 0;
    int rcnt = 0;

    string ans = "";

    for(int i = 0; i < s.size(); i++){
        if(s[i] == ')'){
            rcnt++;
            for(int j = i + 1; j < s.size(); j++){
                if(s[j] == ')'){
                    rcnt++;
                } else {
                    i = j - 1;
                    break;
                }
                i = j;
            }

            for(int j = 0; j < rcnt; j++){
                ans += '(';
            }
            for(int j = 0; j < rcnt; j++){
                ans += ')';
            }
            rcnt = 0;
        } else {
            lcnt++;
            for(int j = i + 1; j < n; j++){
                if(s[j] == '('){
                    lcnt++;
                } else {
                    i = j - 1;
                    break;
                }
                i = j;
            }

            for(int j = i; j < n; j++){
                if(s[j] == ')'){
                    rcnt++;
                } else {
                    i = j;
                    break;
                }
                i = j;
            }

            cout << rcnt << lcnt << endl;

            for(int j = 0; j < max(rcnt, lcnt); j++){
                ans += '(';
            }
            for(int j = 0; j < max(rcnt, lcnt); j++){
                ans += ')';
            }

            rcnt = 0;
            lcnt = 0;

        }
    }

    cout << ans << endl;
}