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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contests
{
    enum Variant
    {
        Index,
        Minimal,
        Composition,
        Sequence,
        Add,
    }
    class Holder
    {
        public Variant Variant {get;set;}
        public int Start { get; set; }
        public int End { get; set; }
        public char Added { get; set; }
        public int Count { get; set; }
        public Holder(int i)
        {
            Variant = Variant.Index;
            Start = End = i;
        }
        public Holder(int start, int end, Variant variant = Variant.Composition)
        {
            Variant = variant;
            Start = start;
            End = end;
        }
        public Holder(char c, int count)
        {
            Variant = Variant.Add;
            Added = c;
            Count = count;
        }


        public bool IsIndex() { return Variant == Variant.Index; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var input = Console.ReadLine();
            var holders = Enumerable.Range(0, n).Select(i => new Holder(i)).ToList();
            while (true)
            {
                var newHolders = new List<Holder>();
                for(int i = 0; i < holders.Count; i++)
                {
                    if(holders[i].IsIndex())
                    {
                        if(i+1 < holders.Count && input[holders[i].Start] == '(')
                        {
                            if(holders[i+1].IsIndex() && input[holders[i+1].Start] == ')')
                            {
                                newHolders.Add(new Holder(holders[i].Start, holders[i + 1].End, Variant.Minimal));
                                i++;
                            }
                            else if(i+2 < holders.Count && !holders[i+1].IsIndex() && holders[i+2].IsIndex() && input[holders[i+2].Start] == ')')
                            {
                                newHolders.Add(new Holder(holders[i].Start, holders[i + 2].End, Variant.Composition));
                                i += 2;

                            }
                            else
                            {
                                newHolders.Add(holders[i]);
                            }
                        }
                        else
                        {
                            newHolders.Add(holders[i]);
                        }
                    }
                    else
                    {
                        if(i + 1 < holders.Count && !holders[i+1].IsIndex())
                        {
                            var h1 = holders[i];
                            var h2 = holders[i + 1];
                            newHolders.Add(new Holder(h1.Start, h2.End));
                            i++;
                        }
                        else
                        {
                            newHolders.Add(holders[i]);
                        }
                    }
                }
                if (holders.Count <= newHolders.Count) break;
                holders = newHolders;
            }
            int left = 0;
            for(int i = 0; i < holders.Count(); i++)
            {
                if (holders[i].IsIndex())
                {
                    if(input[holders[i].Start] == '(')
                    {
                        left++;
                    }
                    else if(left > 0)
                    {
                        holders.Insert(i, new Holder(')', left));
                        left = 0;
                        i++;
                    }
                }
            }
            if(left > 0)
            {
                holders.Add(new Holder(')', left));
            }
            int right = 0;
            for(int i = holders.Count() - 1; i >= 0 ; i--)
            {
                if (holders[i].IsIndex())
                {
                    if(input[holders[i].Start] == ')')
                    {
                        right++;
                    }
                    else if(right > 0)
                    {
                        holders.Insert(i + 1, new Holder(')', right));
                        right = 0;
                    }
                }
            }
            if(right > 0)
            {
                holders.Insert(0, new Holder('(', right));
            }
            foreach (var holder in holders)
            {
                if(holder.Variant == Variant.Index)
                {
                    Console.Write(input[holder.Start]);
                }else if(holder.Variant == Variant.Add)
                {
                    for (int i = 0; i < holder.Count; i++) Console.Write(holder.Added);
                }else
                {
                    for (int i = holder.Start; i <= holder.End; i++) Console.Write(input[i]);
                }
            }
            Console.WriteLine();
        }
    }
}