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

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



Sample input:
3
())


Sample output:
(())

Submission status: Runtime Error

Solution code:
import math
n = int(input())
lis = []
cou = 0
ans = 0
li = list(map(int,input().split()))
for i in range(n):
    if li[i] >= 3200:
        cou += 1
    else:
        if math.floor(li[i] / 400) not in lis:
            ans += 1
print(ans,min(8,ans + cou))

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())
j = []
now = 0
unclosen = 0
opened = 0
for i in s:
    if i == ")":
        now -= 1
        if opened:
            opened -= 1
        else:
            unclosen += 1
    else:
        now += 1
        opened += 1
j = np.array(j)
print("(" * unclosen + "".join(s) + ")" * opened)

Submission status: Runtime Error

Solution code:
n = int(input())
N = input()
L = 0
R = 0
num = 0
while N[num] == ')':
    L += 1
    num += 1
num = n-1
while N[num] == '(':
    R += 1
    num -= 1
a = N.count('(')
b = len(N) - a
c = a - b + L -R
if (c) < 0:
    for i in range(abs(c)):
        N = '(' + N
elif (c) > 0:
    for i in range(abs(c)):
        N = N + ')'
print('('*L+N+')'*R)

Submission status: Runtime Error

Solution code:
n = int(input())
N = input()
num = 0
while N[num] == ')':
    N = '(' + N
    num += 2
a = N.count('(')
b = len(N)-a
if (a - b) < 0:
    for i in range(b-a):
        N = '(' + N
else:
    for i in range(a-b):
        N = N + ')'

print(N)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
stack=[]
for c in s:
    if len(stack)>0 and stack[-1]=='(' and c==')':
        stack.pop()
    else:
        stack.append(c)
if len(stack)==0:
    print(s)
else:
    l=0
    r=0
    for c in stack:
        if c==')':
            l+=1
        else:
            r+=1
print('('*l+s+')'*r)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(input())
b = []
cou1=0

for i in a:
    if i=="(":
        cou1 += 1
        b.append("(")
    else:
        b.append(")")
        if cou1==0:
            b.insert(0,"(")
            continue
        cou1 -= 1

for _ in range(cou1):
    b.append(")")

print(*b,sep='')n = int(input())
a = list(input())
b = []
cou1=0

for i in a:
    if i=="(":
        cou1 += 1
        b.append("(")
    else:
        b.append(")")
        if cou1==0:
            b.insert(0,"(")
            continue
        cou1 -= 1

for _ in range(cou1):
    b.append(")")

print(*b,sep='')

Submission status: Runtime Error

Solution code:
N = input()
string = input()
arrange = string
while True:
    after_arrange = arrage.replace('()','')
    if after_arrange == arrage:
        break
    arrange = after_arrange
left = after_arrange.count(')')
right = after_arrange.count('(')
print('('*left+string+')'*right)

Submission status: Runtime Error

Solution code:
N = input()
string = input()
arrange = string
while True:
    after_arrange = arrage.replace('()','')
    if after_arrange == arrage
        break
    arrange = after_arrange
left = after_arrange.count(')')
right = after_arrange.count('(')
print('('*left+string+')'*right)

Submission status: Runtime Error

Solution code:
N = input()
string = input()
arrange = string.replace('()','')
while True:
    if arrange == arrage.replace('()',''):
        break
    arrange = arrage.replace('()','')
left = arrange.count(')')
right = arrange.count('(')
print('('*left+string+')'*right)

Submission status: Runtime Error

Solution code:

N=int(input())

a=[int(i) for i in input().split()]

color=[0,0,0,0,0,0,0,0]
alles=0

for i in range(N) :
    
    if a[i]<400 :
        color[0]+=1
    elif a[i]<800 :
        color[1]+=1
    elif a[i]<1200 :
        color[2]+=1
    elif a[i]<1600 :
        color[3]+=1
    elif a[i]<2000 :
        color[4]+=1
    elif a[i]<2400 :
        color[5]+=1
    elif a[i]<2800 :
        color[6]+=1
    elif a[i]<3200 :
        color[7]+=1
    else :
        alles+=1

        
while 0 in color :color.remove(0)
eight=int(8)

if alles>=8 :
    print(eight," ",eight)
elif alles+len(color)>=8 :
    if alles>=len(color) :
        print(alles," ",eight)
    else :
        print(len(color)," ",eight)
else :
    if alles>=len(color) :
        print(alles," ",len(color)+alles)
    else :
        print(len(color)," ",len(color)+alles)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys

N = input()
S = input()

res = ''
left, right = 0, 0
lit = S.split(')(')
for i, l in enumerate(lit):
    if len(lit) > 1:
        kakko = l + ')' if i % 2 == 0 else '(' + l
    j = 0
    while True:
        if len(kakko)-1 < j:
            break
        if kakko[j] == '(':
            if len(kakko) - 2 > j:
                if kakko[j+1] == ')':
                    res += '()'
                    j += 2
                else:
                    left += 1
                    j += 1
            else:
                left += 1
                j += 1
        else:
            if left != 0:
                left -= 1
            res = '(' + res + ')'
            j += 1
    part = '(' * left + ')' * left
    res  += part
print(res)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    S = input()

    s = S
    while True:
        s = s.replace('()', '')
        if s.count('()') == 0:
            break

    left = ''
    right = ''
    l = s.count('(')
    r = s.count(')')
    if s[0] == ')':
        left = '(' * r
    if s[-1] == '(':
        right = ')' * l

    print('{}{}{}'.format(left, S, right))

main()

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
a1 = 0
a2 = 0
while S[a1] =="(":
    a1 += 1
while S[N - a2 - 1] == ")":
    a2 += 1

S_ = S[a1:N - a2]

S1 = "".join(S_.split(")("))
S2 = "".join(S1.split(")("))
d = (S1 == S_ or S1 == S2)
a = S2.count("(") + 2 - d - a2
b = S2.count(")") + 2 - d - a1

if b < 0 or a < 0:
    m = min(a, b)
    a -= m
    b -= m

print("(" * b + S + ")" * a)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
a1 = 0
a2 = 0
while S[a1] =="(":
    a1 += 1
while S[N - a2 - 1] == ")":
    a2 += 1

S_ = S[a1:N - a2]

S1 = "".join(S_.split(")("))
S2 = "".join(S1.split(")("))
d = (S1 == S_ or S1 == S2)
a = S2.count("(") + 2 - d
b = S2.count(")") + 2 - d

b -= a1
a -= a2

if b < 0:
    a -= b
    b = 0
if a < 0:
    b -= a
    a = 0

print("(" * b + S + ")" * a)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
a1 = 0
a2 = 0
while S[a1] =="(":
    a1 += 1
while S[N - a2 - 1] == ")":
    a2 += 1

S_ = S[a1:N - a2]

S1 = "".join(S_.split(")("))
S2 = "".join(S1.split(")("))
d = (S1 == S_ or S1 == S2)
a = S2.count("(") + 2 - d
b = S2.count(")") + 2 - d

b -= a1
a -= a2

if b < 0:
    a -= b
    b = 0
if a - a2 < 0:
    b -= a
    a = 0

print("(" * b + S + ")" * a)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
a1 = 0
a2 = 0
while S[a1] =="(":
    a1 += 1
while S[N - a2 - 1] == ")":
    a2 += 1

S_ = S[a1:N - a2]

S1 = "".join(S_.split(")("))
S2 = "".join(S1.split(")("))
d = (S1 == S_ or S1 == S2)
a = S2.count("(") + 2 - d
b = S2.count(")") + 2 - d

if b - a1 < 0:
    a += a1 - b - a2
    b = 0
elif a - a2 < 0:
    b += a2 - a
    a = 0
else:
    b -= a1
    a -= a2

print("(" * b + S + ")" * a)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())
t = s[::-1]
u = s[0::]
#できるだけ左から(を追加 &右から)を追加

#右から数えて最初に(より左にある)の数　と　)より右にある(の数？
v = []
si = s.index(")")
ti = t.index("(")

stock = 0
i = 0

for i in s:
    if i=="(":
        stock+=1
        v.append("(")
    else:
        if stock ==0:
            v.insert(0,"(")
            v.append(")")
        else:
            stock -= 1
            v.append(")")

for i in range(stock):
    v.append(")")

print(*v,sep='')

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
ls = list(S)
b = ls.index('(')
ls.reverse()
a = ls.index(')')
if a + b != N:
	sli = ls[a:N-b]
	k = sli.count(')')
	l = sli.count('(')
	m = k - l
else:
	k = 0
	l = 0
	m = 0
if m > 0:
	n = m + b
	ls.append('(' * n)
	ls.reverse()
	ls.append(')' * a)
	print(''.join(ls))
else:
	n = a - m
	ls.append('(' * b)
	ls.reverse()
	ls.append(')' * n)
	print(''.join(ls))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
ls = list(S)
b = ls.index('(')
ls.reverse()
a = ls.index(')')
if a + b != N:
	sli = ls[a:N-b]
	k = sli.count(')')
	l = sli.count('(')
	m = k - l
else:
	k = 0
	l = 0
if m > 0:
	n = m + b
	ls.append('(' * n)
	ls.reverse()
	ls.append(')' * a)
	print(''.join(ls))
else:
	n = a - m
	ls.append('(' * b)
	ls.reverse()
	ls.append(')' * n)
	print(''.join(ls))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
ls = list(S)
b = ls.index('(')
ls.reverse()
a = ls.index(')')
sli = ls[a:N-b]
k = sli.count(')')
l = sli.count('(')
m = k - l
if m > 0:
	n = m + b
	ls.append('(' * n)
	ls.reverse()
	ls.append(')' * a)
	print(''.join(ls))
else:
	n = a - m
	ls.append('(' * b)
	ls.reverse()
	ls.append(')' * n)
	print(''.join(ls))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
ls = list(S)
ls.reverse()
a = ls.index(')')
sli = ls[a:N]
k = sli.count(')')
l = sli.count('(')
m = k - l
if m > 0:
	ls.append('(' * m)
	ls.reverse()
	ls.append(')' * a)
	print(''.join(ls))
else:
	n = a - m
	ls.reverse()
	ls.append(')' * n)
	print(''.join(ls))

Submission status: Runtime Error

Solution code:
N=int(raw_input())
S=raw_input()

op=0
cl=0

i=0
j=len(S)-1
while i<=j:
    if i==j:
        if S[i]=='(':
            cl+=1
        else:
            op+=1
        break
    while S[i]!='(':
        i+=1
        op+=1
    while S[j]!=')':
        j-=1
        cl+=1
    if i<j:
        i+=1
        j-=1

r='('*op+S+')'*cl

print r

Submission status: Runtime Error

Solution code:
N=int(raw_input())
S=raw_input()

op=S.count('(')
cl=S.count(')')

if op>cl:
    r=S+')'*(op-cl)
elif cl>op:
    r='('*(cl-op)+S

print r