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 Rust (1.42.0) (judge verdict: Accepted)

#[allow(unused_macros, dead_code)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros, dead_code)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros, dead_code)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(dead_code)]
struct UnionFind {
    parent: Vec<usize>,
    rank: Vec<usize>,
    size: Vec<usize>,
}

#[allow(dead_code)]
impl UnionFind {
    fn new(n: usize) -> UnionFind {
        let mut p = vec![0; n];
        for i in 0..n {
            p[i] = i;
        }
        return UnionFind {
            parent: p,
            rank: vec![0; n],
            size: vec![1; n],
        };
    }

    fn find(&mut self, x: usize) -> usize {
        if x == self.parent[x] {
            x
        } else {
            let p = self.parent[x];
            let pr = self.find(p);
            self.parent[x] = pr;
            pr
        }
    }

    fn same(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }

    fn unite(&mut self, a: usize, b: usize) {
        let a_root = self.find(a);
        let b_root = self.find(b);
        if a_root == b_root {
            return;
        }
        if self.rank[a_root] > self.rank[b_root] {
            self.parent[b_root] = a_root;
            self.size[a_root] += self.size[b_root];
        } else {
            self.parent[a_root] = b_root;
            self.size[b_root] += self.size[a_root];
            if self.rank[a_root] == self.rank[b_root] {
                self.rank[b_root] += 1;
            }
        }
    }

    fn get_size(&mut self, x: usize) -> usize {
        let root = self.find(x);
        self.size[root]
    }
}

const MOD_P: usize = 1000000007;

#[allow(dead_code)]
// fact(n) = n! mod p
fn fact(n: usize) -> usize {
    let mut acc = 1;
    for i in 1..n + 1 {
        acc = acc * i % MOD_P;
    }
    acc
}

#[allow(dead_code)]
fn mod_pow(b: usize, mut e: usize) -> usize {
    let mut base = b;
    let mut acc = 1;

    while e > 1 {
        if e % 2 == 1 {
            acc = acc * base % MOD_P;
        }
        e /= 2;
        base = base * base % MOD_P;
    }

    if e == 1 {
        acc = acc * base % MOD_P;
    }

    acc
}

#[allow(dead_code)]
fn comb(n: usize, r: usize) -> usize {
    // nCr = n! / (r! (n-r)!) = n! (r!)^(p-2) ((n-r)!)^(p-2)
    let x = ((n - r + 1)..(n + 1)).fold(1, |p, x| p * x % MOD_P);
    let y = mod_pow(fact(r), MOD_P - 2);
    x * y % MOD_P
}

#[derive(Clone, Copy, Debug)]
struct GF(usize);

impl std::ops::Add for GF {
    type Output = GF;
    fn add(self, rhs: GF) -> Self::Output {
        let mut d = self.0 + rhs.0;
        if d >= MOD_P {
            d -= MOD_P;
        }
        GF(d)
    }
}

impl std::ops::AddAssign for GF {
    fn add_assign(&mut self, rhs: GF) {
        *self = *self + rhs;
    }
}

impl std::ops::Sub for GF {
    type Output = GF;
    fn sub(self, rhs: GF) -> Self::Output {
        let mut d = self.0 + MOD_P - rhs.0;
        if d >= MOD_P {
            d -= MOD_P;
        }
        GF(d)
    }
}

impl std::ops::SubAssign for GF {
    fn sub_assign(&mut self, rhs: GF) {
        *self = *self - rhs;
    }
}

impl std::ops::Mul for GF {
    type Output = GF;
    fn mul(self, rhs: GF) -> Self::Output {
        let mut d = self.0 * rhs.0;
        d %= MOD_P;
        GF(d)
    }
}

impl std::ops::MulAssign for GF {
    fn mul_assign(&mut self, rhs: GF) {
        *self = *self * rhs;
    }
}

impl std::fmt::Display for GF {
    fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[allow(dead_code)]
impl GF {
    pub fn new(n: usize) -> GF {
        GF(n % MOD_P)
    }
    pub fn zero() -> GF {
        GF(0)
    }
    pub fn one() -> GF {
        GF(1)
    }
    pub fn pow(self, mut e: usize) -> GF {
        let mut acc = GF::one();
        let mut b = self;
        while e > 1 {
            if e % 2 == 1 {
                acc *= b;
            }
            b *= b;
            e /= 2;
        }
        if e == 1 {
            acc *= b;
        }
        acc
    }
    pub fn fact(self) -> GF {
        let mut acc = GF::one();
        for i in 1..=self.0 {
            acc *= GF::new(i);
        }
        acc
    }
    pub fn inv(self) -> GF {
        self.pow(MOD_P - 2)
    }
    pub fn comb(n: GF, r: GF) -> GF {
        // nCr = n! / (r! (n-r)!) = n! (r!)^(p-2) ((n-r)!)^(p-2)
        let x = ((n.0 - r.0 + 1)..=n.0).fold(GF::one(), |p, x| p * GF(x));
        let y = r.fact().inv();
        x * y
    }
}

#[allow(dead_code)]
#[derive(Debug)]
struct MemComb {
    inv: Vec<GF>,
    fact: Vec<GF>,
    factinv: Vec<GF>,
}

#[allow(dead_code)]
impl MemComb {
    pub fn new(n: usize) -> MemComb {
        let mut inv = vec![GF::one(); n + 1];
        let mut fact = vec![GF::one(); n + 1];
        let mut factinv = vec![GF::one(); n + 1];

        for i in 2..=n {
            fact[i] = fact[i - 1] * GF(i);
        }

        factinv[n] = fact[n].inv();
        for i in (1..n).rev() {
            factinv[i] = factinv[i + 1] * GF(i + 1); // 1/n! = 1/(n+1)! * (n+1)
            inv[i] = factinv[i] * fact[i - 1]; // 1/n = 1/n! * (n-1)!
        }
        inv[n] = factinv[n] * fact[n - 1];

        MemComb { inv, fact, factinv }
    }
    pub fn comb(&self, n: usize, r: usize) -> GF {
        if n >= r {
            self.fact[n] * self.factinv[r] * self.factinv[n - r]
        } else {
            GF::zero()
        }
    }
}

#[allow(dead_code)]
fn gcd(a: usize, b: usize) -> usize {
    if b > a {
        gcd(b, a)
    } else if a % b == 0 {
        b
    } else {
        gcd(b, a % b)
    }
}

#[allow(dead_code)]
fn getline() -> String {
    let mut __ret = String::new();
    std::io::stdin().read_line(&mut __ret).ok();
    return __ret;
}

#[allow(unused_imports)]
use std::cmp::{max, min};

fn main() {
    input! {
        n: usize,
        s: chars,
    }
    let mut unbalanced_right = 0;
    let mut cur = 0;
    for &c in &s {
        if c == '(' {
            cur += 1;
        } else if cur > 0 {
            cur -= 1;
        } else {
            unbalanced_right += 1;
        }
    }
    let unbalanced_left = cur;

    for _ in 0..unbalanced_right {
        print!("(");
    }
    for c in s {
        print!("{}", c);
    }
    for _ in 0..unbalanced_left {
        print!(")");
    }
}