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 Scala (2.11.7) (judge verdict: Accepted)

import java.io.{BufferedReader, IOException, InputStream, InputStreamReader}
import java.util.StringTokenizer

import scala.collection.mutable
import scala.util.Sorting
import math.{min, max, abs}

object Main {
  def solve(): Unit = {
    val sc = new InputReader(System.in)
    val N = sc.nextInt()
    val S = sc.next

    sealed trait Part
    case class Left(v: Int) extends Part
    case class Right(v: Int) extends Part
    case class Pair(l: Int, r: Int)

    def step(cs: List[Char], pre: Char, cnt: Int, acc: List[Part]): List[Part] = {
      def mkPart = pre match {
        case '(' => Left(cnt)
        case ')' => Right(cnt)
      }

      cs match {
        case Nil => mkPart :: acc
        case c :: remain =>
          c match {
            case `pre` => step(remain, c, cnt + 1, acc)
            case _ => step(remain, c,1, mkPart :: acc)
          }
      }
    }

    val parts = step(S.drop(1).toCharArray.toList, S.charAt(0), 1, Nil).reverse

    def step1(ps: List[Part], left: Option[Left], acc: List[Pair]): List[Pair] = ps match {
      case Nil => left match {
        case None => acc
        case Some(Left(l)) => Pair(l, 0) :: acc
      }
      case Right(r) :: remain =>
        val l = left.map(_.v) getOrElse 0
        step1(remain, None, Pair(l, r) :: acc)

      case (l: Left) :: remain =>
        step1(remain, Some(l), acc)

    }

    val pairs = step1(parts, None, Nil).reverse

    var r = 0 // )の数
    var l = 0 // (の数
    pairs foreach { p =>
      if (p.l > p.r) {
        l += p.l - p.r
      } else if (p.r > p.l) {
        val r0 = p.r - p.l
        if (r0 > l) {
          r += r0 - l
          l = 0
        } else {
          l -= r0
        }
      }
    }
    println("(" * r + S + ")" * l)
  }

  def main(args: Array[String]): Unit = {
    solve()
  }



  class InputReader(val stream: InputStream) {
    val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    var tokenizer: StringTokenizer = null

    def next: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next.toInt
    def nextLong(): Long = next.toLong
    def nextChar(): Char = next.charAt(0)
  }

}