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 (Python):

def get_ln_inputs():
    return input().split()


def main():
    _ = get_ln_inputs()
    S = get_ln_inputs()[0]

    level = 0
    index = 0
    while index < len(S):
        if S[index] == "(":
            index += 1
            level += 1
            continue
        if S[index] == ")":
            if level > 0:
                index += 1
                level -= 1
                continue
            S = "(" + S
            index += 2
    print(S + (")" * level))


main()

Solution (Python):
n = int(input())
s = input()
a = s
f = 0
for c in a:
	if c=="(":
		f+=1
	else:
		if f>0:
			f-=1
		else:
			s = "(" + s
for i in range(f):
	s = s + ")"

print(s)

Solution (Python):
N = int(input())
S = input().rstrip()

def solve(N, S):
    n_close = 0
    max_n_close = 0
    for s in S:
        if s == ')':
            n_close += 1
            max_n_close = max(max_n_close, n_close)
        if s == '(':
            n_close -= 1
    SS = '(' * max_n_close + S
    n_open = SS.count('(')
    n_close = SS.count(')')
    SSS = SS + ')' * (n_open - n_close)
    return SSS

print(solve(N, S))

Solution (Python):
N = int(input())
S = input()

l = ""
r = ""
cnt = 0
for i in S :
	if i == '(' :
		cnt += 1
	else :
		cnt -= 1
	
	if cnt < 0 :
		cnt += 1
		l += '('

r = ')' * cnt

print(l + S + r)

Solution (Python):
input()
S = raw_input()
c = 0
p = 0
for s in S:
    if s=="(":
        c += 1
    else:
        if c==0:
            p += 1
        else:
            c -= 1
print "("*p+S+")"*c

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

_ = input()
S = input()
stack = 0
made = ''
for ch in S:
    
    if ch == '(':
        stack += 1
        made += '('
    elif ch == ')':
        if stack == 0:
            made = '(' + made + ')'
        else:
            made += ')'
            stack -= 1
made += ')' * stack
print(made)

Solution (Python):
N=raw_input()
S=raw_input()

pre=""
cnt=0
for s in S:
    if s == "(":
        cnt += 1
    else:
        if cnt == 0:
            pre += "("
        else:
            cnt -= 1

suf=")"*cnt

print pre+S+suf

Solution (Python):


input()
s = input()
balance = 0
left = 0
for c in s:
    if c == '(':
        balance += 1
    elif balance == 0:
        left += 1
    else:
        balance -= 1
right = balance
print('(' * left + s + ')' * right)

Solution (Python):
n=int(input())
s=input()

op,cl=0,0
for c in s:
    if c=='(':
        op+=1
    else:
        if op>0:
            op-=1
        else:
            cl+=1
print(('('*cl)+s+(')'*op))

Solution (Python):
N = input()
S = raw_input()

l, r = 0, 0
tmp = 0
for i in xrange(N):
    if S[i] == '(':
        tmp += 1
    else:
        tmp -= 1
        if tmp < 0:
            l += 1
            tmp = 0

tmp = 0
for i in xrange(N-1, -1, -1):
    if S[i] == ')':
        tmp += 1
    else:
        tmp -= 1
        if tmp < 0:
            r += 1
            tmp = 0
print '(' * l + S + ')' * r

Solution (Python):
import sys
n = map(int, sys.stdin.readline().split())
a = sys.stdin.readline().strip()
res = a
h = 0
for i in range (len(a)):
  if a[i] == '(':
    h += 1
  else:
    h -= 1
  if h < 0:
    res = '(' + res
    h = 0

print res + ')' * h

Solution (Python):
n = input()
s = raw_input()
right = 0
for i in xrange(n):
    if s[i] == '(':
        right += 1
    else:
        right = max(right-1, 0)
left = 0
for i in xrange(n-1, -1, -1):
    if s[i] == ')':
        left += 1
    else:
        left = max(left-1, 0)
print '('*left+s+')'*right

Solution (Python):
n = int(raw_input())
s = raw_input()
m = 0
c = 0
for si in s:
    if si == "(":
        c += 1
    else:
        c -= 1
    m = min(m, c)

ans = ""
if m < 0:
    ans += "(" * (-m)
    c += -m

if c < 0:
    ans = ans + "(" * (-c) + s
else:
    ans = ans + s + ")" * c

print ans

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
s = ns().strip()
h = 0
minh = 0
for c in s:
    if c == "(":
        h += 1
    else:
        h -= 1
    minh = min(minh, h)
print("("*(-minh) + s + ")"*(h-minh))