ZhenlongDai/ACPR huggingface dataset:
Problem: p03696
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N=int(input())
S=input()
d = [0]*(N+1) 
              
for i in range(1, N+1):
    d[i] = S[:i].count("(") - S[:i].count(")")
x = min(d)

if x == 0 and d[N] == 0:
    print(S)
    exit()

ans = "("*max(abs(x), abs(d[N]-x)) + S + ")"*min(abs(x), abs(d[N]-x))
print(ans)

Failing test case input:
8
())(((((

Expected output:
(())((((()))))


Actual output:
(((((())((((()


Fixed submission (verdict: Accepted):
N=int(input())
S=input()
d = [0]*(N+1) 
              
for i in range(1, N+1):
    d[i] = S[:i].count("(") - S[:i].count(")")
x = min(d)

if x == 0 and d[N] == 0:
    print(S)
    exit()

ans = "("*abs(x) + S + ")"*(d[N]+abs(x))
print(ans)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
S=input()
stack= []
ans=""

for i in range(N):
  if S[i]=="(":
    stack.append(S[i])
  else:
    if len(stack)>0:
      ans = ans + stack.pop()+  ")"
    else:
      ans = "(" + ans + ")"

nokori=len(stack)
ans = nokori * "(" + ans + nokori * ")"

print(ans)

Failing test case input:
8
))()(()(

Expected output:
(())()(()())


Actual output:
(((())()()))


Fixed submission (verdict: Accepted):
N=int(input())
S=input()
stack= []
ans=""
L=0
R=0

for i in range(N):
  if S[i]=="(":
    stack.append(S[i])
  else:
    if len(stack)>0:
      stack.pop()
    else:
      L += 1

R=len(stack)
ans = L * "(" + S + R * ")"

print(ans)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys

stdin = sys.stdin

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

N = ni()
S = ns()

X = [0]*(len(S)-1)
for i in range(len(S)-1):
    if(S[i] == '('):
        X[i] = 1
    else:
        X[i] = -1

x = 0
d = [0]*(len(S))

for i in range(1, len(S)):
    d[i] = sum(X[0:i])

print ('('*-min(d)+S+')'*(d[len(S)-1]-min(d)))

Failing test case input:
8
)(()()((

Expected output:
()(()()(()))


Actual output:
()(()()((
)))


Fixed submission (verdict: Accepted):
import sys

stdin = sys.stdin

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

N = ni()
S = ns().strip()

d = 0
mind = 0

for i in range(len(S)):
    if(S[i] == '('):
        d += 1
    else:
        d -= 1
    mind = min(mind,d)
print ('('*(-mind) + S + ')'*(d-mind))
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from copy import deepcopy

N = int(input())
S = list(input())
newS = deepcopy(S)

i = 0
while 1:
    if i >= len(S):
        break
    if S[i] == "(":
        j = i
        while 1:
            if j >= len(S):
                break
            if S[j] == ")":
                S.pop(j)
                S.pop(i)
            j += 1
    i += 1

y = S.count("(")
x = S.count(")")

print("(" * x + "".join(newS) + ")" * y)


Failing test case input:
8
))(())()

Expected output:
(())(())()


Actual output:
((())(())())


Fixed submission (verdict: Accepted):
from copy import deepcopy

N = int(input())
S = list(input())
newS = deepcopy(S)

i = 0
while 1:
    if i >= len(S):
        break
    if S[i] == "(":
        j = i
        while 1:
            if j >= len(S):
                break
            if S[j] == ")":
                S.pop(j)
                S.pop(i)
                i = -1
                break
            j += 1
    i += 1

y = S.count("(")
x = S.count(")")

print("(" * x + "".join(newS) + ")" * y)

Problem: p03696
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

def main():
    N = int(input())
    T = input()
    if N == 1:
      print('()')
      exit()
    S = []
    for s in T:
        if s == '(':
            S.append(1)
        else:
            S.append(-1)
    S = list(accumulate(S))
    print(S)
    minS = min(S)
    for i in range(N):
        S[i] -= minS
    print('('*(-minS) + T + ')'*S[-1])
    

if __name__ == "__main__":
    main()


Failing test case input:
8
))((()()

Expected output:
(())((()()))


Actual output:
[-1, -2, -1, 0, 1, 0, 1, 0]
(())((()()))


Fixed submission (verdict: Accepted):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

def main():
    N = int(input())
    T = input()
    S = []
    for s in T:
        if s == '(':
            S.append(1)
        else:
            S.append(-1)
    S = list(accumulate(S))
    
    minS = min(S)
    if minS < 0:
        for i in range(N):
            S[i] -= minS
    print('('*max(0,-minS) + T + ')'*S[-1])

if __name__ == "__main__":
    main()
Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    S=input()
    
    st=0
    a=""
    b=""
    for i in range(N):
        s=S[i]
        if st==0 and s==")":
            a+="("
        elif s=="(":
            st+=1
        else:
            st-=1
    b+=")"*st
    print(a+S+b)

main()


Failing test case input:
8
)())((()

Expected output:
(()())((()))


Actual output:
(()())((()
))


Fixed submission (verdict: Accepted):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    S=input()
    
    st=0
    a=""
    b=""
    for i in range(N):
        s=S[i]
        if st==0 and s==")":
            a+="("
        elif s=="(":
            st+=1
        else:
            st-=1
    b+=")"*st
    print(a+S+b)

main()

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()

tmp = []
ans = ""
for i in s:
    if i == "(":
        tmp.append("(")
    else:
        if len(tmp) == 0:
            ans = "(" + ans + ")"
        else:
            ans += tmp.pop() + ")"
print(ans)

Failing test case input:
6
)((()(

Expected output:
()((()()))


Actual output:
()()


Fixed submission (verdict: Accepted):
n = int(input())
s = input()

tmp = 0
a = 0
for i in s:
    if i == "(":
        tmp -= 1
    else:
        tmp += 1
    a = max(a, tmp)
ans = a * "(" + s
print(ans + ")" * (ans.count("(") - ans.count(")")))

Problem: p03696
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    S = input().rstrip()

    ans = ""
    n_left = 0
    for s in S:
        if s == "(":
            ans = "".join((ans, "("))
            n_left += 1
        else:
            if n_left > 0:
                n_left -= 1
                ans = "".join((ans, ")"))
            else:
                ans = "".join((ans, "()"))
    ans = "".join((ans, ")" * n_left))

    print(ans)


if __name__ == "__main__":
    main()


Failing test case input:
8
)())((()

Expected output:
(()())((()))


Actual output:
()()()((()))


Fixed submission (verdict: Accepted):
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    S = input().rstrip()

    ans = ""
    n_left = 0
    n_right = 0
    for s in S:
        if s == "(":
            n_left += 1
        else:
            if n_left > 0:
                n_left -= 1
            else:
                n_right += 1

    ans = "".join(("(" * n_right, S, ")" * n_left))
    print(ans)


if __name__ == "__main__":
    main()
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()

pares = S[:]

counter = 0
pares_num = []
for s in pares:
    if s == '(':
        counter += 1
        pares_num.append(counter)
    else:
        counter -= 1
        pares_num.append(counter)
        
min_num = min(pares_num)
if min_num < 0:
    pares = '(' * (-1*min_num) + pares
    

counter = 0
pares_num = []
for s in pares:
    if s == '(':
        counter += 1
        pares_num.append(counter)
    else:
        counter -= 1
        pares_num.append(counter)



zeros = [i for i, x in enumerate(pares_num) if x == 0]
if zeros:
    max_num = max(pares_num[zeros[-1]:])
    pares += ')' * max_num
else:
    max_num = max(pares_num)
    pares += ')' * max_num

print(pares)

Failing test case input:
6
)))(()

Expected output:
((()))(())


Actual output:
((()))(()))


Fixed submission (verdict: Accepted):
N = int(input())
S = input()

pares = S[:]

counter = 0
pares_num = []
for s in pares:
    if s == '(':
        counter += 1
        pares_num.append(counter)
    else:
        counter -= 1
        pares_num.append(counter)
        
min_num = min(pares_num)
if min_num < 0:
    pares = '(' * (-1*min_num) + pares
    

counter = 0
pares_num = []
for s in pares:
    if s == '(':
        counter += 1
        pares_num.append(counter)
    else:
        counter -= 1
        pares_num.append(counter)



pares += ')' * pares_num[-1]


print(pares)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = list(input())

look = 0
left = 0
right = 0
insertion = 0

while look < len(s):
    if s[look] == "(":
        right += 1
    elif s[look] == ")":
        left += 1
    if right == left:
        insertion = look
    elif left > right:
        s.insert(insertion,"(")
        right += 1
        look += 1
    look += 1

if right > left:
    for i in range(right-left):
        s.append(")")
for i in range(len(s)):
    if i != len(s) -1:
        print(s[i],end = "")
    else:
        print(s[i])

Failing test case input:
8
)()()())

Expected output:
(()()()())


Actual output:
()()()(())


Fixed submission (verdict: Accepted):
n = int(input())
s = list(input())
left = 0
right = 0
i = 0
while i < len(s):
    if s[i] == "(":
        left += 1
    else:
        right += 1
    if left < right:
        s.insert(0,"(")
        left += 1
        i += 1
    i += 1
for i in range(left-right):
    s.append(")")
for i in range(len(s)):
    if i != len(s)-1:
        print(s[i],end="")
    else:
        print(s[i])
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
n = int(input())
s = input()
d = deque(s)

stack = []
for c in s:
  
  if c == "(":
    stack.append(c)
    
  
  else:
    
    if not stack:
      d.appendleft('(')
      
    
    else:
      
      stack.pop()
print("".join(list(d)))




Failing test case input:
8
))())(((

Expected output:
((())())((()))


Actual output:
((())())(((


Fixed submission (verdict: Accepted):
from collections import deque
n = int(input())
s = input()
d = deque(s)

stack = []
for c in s:
  
  if c == "(":
    stack.append(c)
    
  
  else:
    
    if not stack:
      d.appendleft('(')
      
    
    else:
      
      stack.pop()


while stack:
  stack.pop()
  d.append(')')
  
print("".join(list(d)))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()
l_cnt = 0
r_cnt = 0
for i in s:
    if i == '(':
        l_cnt += 1
    else:
        r_cnt += 1

if l_cnt == r_cnt:
    print(s)
    
if l_cnt < r_cnt:
    print('('*(r_cnt - l_cnt) +s)
    
if l_cnt > r_cnt:
    print(s +')'*(l_cnt - r_cnt))

Failing test case input:
6
)))(()

Expected output:
((()))(())


Actual output:
(()))(()


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
l_cnt = 0
r_cnt = 0
l_ans = 0
for i in s:
    if i == ')':
        if l_cnt:
            l_cnt -= 1
        else:
            l_ans += 1
    else:
        l_cnt += 1

print(l_ans*'(' + s + l_cnt*')')

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
_,s=open(0)
x=y=0
for c in s:
	if c=="(":
		x+=1
	else:
		x-=1
	y=max(y,-x)
print("("*y+s+")"*(x+y))

Failing test case input:
8
)(())())

Expected output:
(()(())())


Actual output:
((()(())())



Fixed submission (verdict: Accepted):
input()
s=input()
x=y=0
for c in s:
	if c=="(":
		x+=1
	else:
		x-=1
	y=max(y,-x)
print("("*y+s+")"*(x+y))

Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n=int(input())
s=list(input())

for i in range(n):
    if s[i]==')':
        s[i]=1
    else:
        s[i]=-1

ans=0
v=[]
for i in range(n):
    ans+=s[i]
    if ans>0:
        v.append(-1)
        ans-=1
s=v+s
if ans<0:
    for i in range(abs(ans)):
        s.append(1)
for i in range(len(s)):
    if s[i]==-1:
        s[i]='('
    else:
        s[i]=')'
print(s)
print(''.join(s))
            
            
            
    
        

        
    


Failing test case input:
6
))(()(

Expected output:
(())(()())


Actual output:
['(', '(', ')', ')', '(', '(', ')', '(', ')', ')']
(())(()())


Fixed submission (verdict: Accepted):
n=int(input())
s=list(input())

for i in range(n):
    if s[i]==')':
        s[i]=1
    else:
        s[i]=-1

ans=0
v=[]
for i in range(n):
    ans+=s[i]
    if ans>0:
        v.append(-1)
        ans-=1
s=v+s
if ans<0:
    for i in range(abs(ans)):
        s.append(1)
for i in range(len(s)):
    if s[i]==-1:
        s[i]='('
    else:
        s[i]=')'
print(''.join(s))
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
if __name__ == '__main__':
    N = int(input())
    S = input()

    for i in range(50):
        tmpS = S.replace("()", "")

    left_parenthesis_count = 0
    right_parenthesis_count = 0
    for moji in tmpS:
        if moji == "(":
            left_parenthesis_count += 1
        elif moji == ")":
            right_parenthesis_count += 1

    ans = S
    for i in range(right_parenthesis_count):
        ans = "(" + ans

    for i in range(left_parenthesis_count):
        ans += ")"

    print(ans)


Failing test case input:
8
)()((())

Expected output:
()()((()))


Actual output:
(()()((())))


Fixed submission (verdict: Accepted):
if __name__ == '__main__':
    N = int(input())
    S = input()
    tmpS = S
    for i in range(50):
        tmpS = tmpS.replace("()", "")

    
    left_parenthesis_count = 0
    right_parenthesis_count = 0
    for moji in tmpS:
        if moji == "(":
            left_parenthesis_count += 1
        elif moji == ")":
            right_parenthesis_count += 1

    ans = S
    for i in range(right_parenthesis_count):
        ans = "(" + ans

    for i in range(left_parenthesis_count):
        ans += ")"

    print(ans)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

from collections import deque

N = int(input())
S = input()

ans = deque()
tmp = ''
l = r = 0
for c in S:
    if c == '(':
        l += 1
        tmp += '('
    else:
        if l == 0:
            ans.appendleft('(')
            ans.append(')')
        else:
            r += 1
            tmp += ')'
            if l == r:
                ans.append(tmp)
                l = r = 0
if l > r:
    tmp += ')' * (l-r)
    ans.append(tmp)
print(''.join(ans))


Failing test case input:
8
))()(()(

Expected output:
(())()(()())


Actual output:
(())()()(()())


Fixed submission (verdict: Accepted):


import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

from collections import deque

N = int(input())
S = input()

ans = deque()
tmp = ''
l = r = 0
for c in S:
    if c == '(':
        l += 1
        tmp += '('
    else:
        if l == 0:
            ans.appendleft('(')
            ans.append(')')
        else:
            r += 1
            tmp += ')'
            if l == r:
                ans.append(tmp)
                tmp = ''
                l = r = 0
if l > r:
    tmp += ')' * (l-r)
    ans.append(tmp)
print(''.join(ans))
Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):


n = int(input())

s = input()

now =0
right = 0
for i in range(n):
    if(s[n-1-i]=='('):
        now+=1
    else:
        now-=1
    if(right<now):
        right=now
left=0
for i in range(n):
    if(s[i]=='('):
        now-=1
    else:
        now+=1
    if(left<now):
        left=now

print('('*left +s+ ')'*right)

Failing test case input:
6
()((()

Expected output:
()((()))


Actual output:
((()((()))


Fixed submission (verdict: Accepted):

n = int(input())

s = input()

now =0
right = 0
for i in range(n):
    if(s[n-1-i]=='('):
        now+=1
    else:
        now-=1
    if(right<now):
        right=now
left=0
now=0
for i in range(n):
    if(s[i]=='('):
        now-=1
    else:
        now+=1
    if(left<now):
        left=now

print('('*left +s+ ')'*right)

Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n=int(input())
s=list(input())

for i in range(n):
    if s[i]==')':
        s[i]=1
    else:
        s[i]=-1

ans=0
v=[]
for i in range(n):
    ans+=s[i]
    if ans>0:
        v.append(-1)
        ans-=1
s=v+s
if ans<0:
    for i in range(abs(ans)):
        s.append(1)
for i in range(len(s)):
    if s[i]==-1:
        s[i]='('
    else:
        s[i]=')'
print(*s)
            
            
            
    
        

        
    


Failing test case input:
8
))((()()

Expected output:
(())((()()))


Actual output:
( ( ) ) ( ( ( ) ( ) ) )


Fixed submission (verdict: Accepted):
n=int(input())
s=list(input())

for i in range(n):
    if s[i]==')':
        s[i]=1
    else:
        s[i]=-1

ans=0
v=[]
for i in range(n):
    ans+=s[i]
    if ans>0:
        v.append(-1)
        ans-=1
s=v+s
if ans<0:
    for i in range(abs(ans)):
        s.append(1)
for i in range(len(s)):
    if s[i]==-1:
        s[i]='('
    else:
        s[i]=')'
print(''.join(s))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()
T = []
c = 0
m = 0
for s in S:
  if s == ")":
    c += 1
  else:
    if c > 0:
      m += c 
      c = 0
    c -= 1
print("("*m + S + ")"*(-c))
  

Failing test case input:
6
)()())

Expected output:
(()()())


Actual output:
()()())


Fixed submission (verdict: Accepted):
N = int(input())
S = input()
T = []
c = 0
m = 0
for s in S:
  if s == ")":
    c += 1
  else:
    if c > 0:
      m += c 
      c = 0
    c -= 1
  if c > 0:
    m += c 
    c = 0
print("("*m + S + ")"*(-c))
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()
stack_num = 0
ind = 0
ret = ""
nr = 0
for i in range(ind, n):
    if(s[i] == ")"):
        nr += 1
    else:
        break
if(nr > 0):
    ret += "("*nr
    ret += ")"*nr
ind += nr

stack_num = 0
for i in range(ind, n):
    if(s[i] == "("):
        stack_num += 1
    else:
        stack_num -= 1

if(stack_num >= 0):
    ret += s[ind:n] + ")"*stack_num
else:
    ret += "("*(-stack_num) + s[ind:n]

print(ret)


Failing test case input:
8
))))()))

Expected output:
(((((())))()))


Actual output:
(((())))((()))


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
stack_num = 0
ind = 0
ret = ""
nr = 0
for c in s:
    if(c == "("):
        stack_num += 1
    else:
        stack_num -= 1
    if(stack_num < 0):
        ret += "("
        stack_num += 1
if(stack_num < 0):
    ret += "(" * (-stack_num)
ret += s
if(stack_num >= 0):
    ret += ")" * stack_num
print(ret)

Problem: p03696
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import copy
N = int(input())
S = list(input())
S_ans = copy.deepcopy(S)
app = 0
q = [0]
save = 0
for i in range(N):
    if S[i] == '(':
        q.append(q[-1] + 1)
        if q[-1] == 1:
            save = i
    else:
        q.append(q[-1] - 1)
        if q[-1] == -1:
            S_ans.insert(app + save,"(")
            app += 1
            q[-1] = 0

for i in range(q[-1]):
    S_ans.append(")")
print("".join(S_ans))

Failing test case input:
8
(()))())

Expected output:
(((()))())


Actual output:
((()))(())


Fixed submission (verdict: Accepted):
import copy
N = int(input())
S = list(input())
S_ans = copy.deepcopy(S)
app = 0
q = [0]
save = 0
for i in range(N):
    if S[i] == '(':
        q.append(q[-1] + 1)
        
    else:
        q.append(q[-1] - 1)
        if q[-1] == -1:
            S_ans.insert(app + save,"(")
            app += 1
            q[-1] = 0

for i in range(q[-1]):
    S_ans.append(")")
print("".join(S_ans))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def d_Insertion(N, S):
    
    d = 0
    x = float('inf')
    for c in S:
        if c == '(':
            d += 1
        else:
            d -= 1
        x = min(x, d)
    ans = '(' * (-x) + S + ')' * (d - x)
    return ans

N = int(input())
S = input().strip()
print(d_Insertion(N, S))

Failing test case input:
3
(()

Expected output:
(())


Actual output:
(()


Fixed submission (verdict: Accepted):
def d_Insertion(N, S):
    
    d = 0
    x = 0
    for c in S:
        if c == '(':
            d += 1
        else:
            d -= 1
        x = min(x, d)
    ans = '(' * (-x) + S + ')' * (d - x)
    return ans

N = int(input())
S = input().strip()
print(d_Insertion(N, S))
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
n=input()
l=0;r=0
for i in n:
    if i=="(":
        r+=1
    else:
        if r:
            r-=1
        else:
            l+=1
print("("*r+n+")"*l)

Failing test case input:
8
)())())(

Expected output:
((()())())()


Actual output:
()())())()))


Fixed submission (verdict: Accepted):
N=int(input())
n=input()
l=0;r=0
for i in n:
    if i=="(":
        r+=1
    else:
        if r:
            r-=1
        else:
            l+=1
print("("*l+n+")"*r)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def insertion(S):
    Sc = S
    Scc = Sc.replace("()", "")
    while Scc != Sc:
        Sc = Scc
        Scc = Scc.replace("()", "")

    
    
    if len(Sc) == 0:
        pass
    elif Sc[0] != Sc[-1]:
        pos = Sc.find(")(")
        S = insertion(S[:pos+1]) + insertion(S[pos+1:])
    elif Sc[0] == '(':
        S += ')' * len(Sc)
    else:
        S = '(' * len(Sc) + S

    return S

N = int(input())
print(insertion(input()))


Failing test case input:
8
)()))(()

Expected output:
((()()))(())


Actual output:
()()(())(())


Fixed submission (verdict: Accepted):
def insertion(S):
    Sc = S
    Scc = Sc.replace("()", "")
    while Scc != Sc:
        Sc = Scc
        Scc = Scc.replace("()", "")

    
    
    if len(Sc) == 0:
        Sl, Sr = "", ""
    elif Sc[0] != Sc[-1]:
        pos = Sc.find(")(")
        Sl = insertion(Sc[:pos+1])[0]
        Sr = insertion(Sc[pos+1:])[2]
    elif Sc[0] == '(':
        Sl = ""
        Sr = ')' * len(Sc)
    else:
        Sl = '(' * len(Sc)
        Sr = ""

    return Sl, S, Sr

N = int(input())
s = insertion(input())
print(s[0]+s[1]+s[2])

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
S=input()
sl=list(S)
change=[0]
for i in range(1,N):
  if (sl[i-1]==')')and(sl[i]=='('):
    change.append(i)

change.append(N)
r_add=0
l_add=0
for i in range(len(change)-1):
  A=sl[change[i]:change[i+1]]
  r=0
  l=0
  for a in A:
    if a=='(':
      l+=1
    else:
      r+=1
      
  if r==l:
    continue
  elif r>l:
    l_add+=r-l
    r_add-=r-l
  else:
    r_add+=l-r
    l_add-=l-r
      
print('('*l_add+S+')'*r_add)

Failing test case input:
6
)))))(

Expected output:
((((()))))()


Actual output:
(((()))))(


Fixed submission (verdict: Accepted):
N=int(input())
S=input()
sl=list(S)
change=[0]
for i in range(1,N):
  if (sl[i-1]==')')and(sl[i]=='('):
    change.append(i)

change.append(N)
r_add=0
l_add=0
for i in range(len(change)-1):
  A=sl[change[i]:change[i+1]]
  r=0
  l=0
  for a in A:
    if a=='(':
      l+=1
    else:
      r+=1
      
  if r==l:
    continue
  elif r>l:
    if r_add>0:
      if r_add>=r-l:
        r_add-=r-l
      else:
        l_add+=r-l-r_add
        r_add=0
        
    else:
      l_add+=r-l
      
  else:
    r_add+=l-r
      
print('('*l_add+S+')'*r_add)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()
cnt = [0, 0]
add = [0, 0]
ans = ''
for i in range(n):
    if s[i] == ')':
        cnt[0] += 1
        if cnt[0] > cnt[1]:
            add[0] += 1
    else:
        cnt[1] += 1

print('(' * add[0] + s + ')'*(cnt[1] -cnt[0] + add[0]))


Failing test case input:
6
)(()))

Expected output:
(()(()))


Actual output:
((()(())))


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
cnt = [0, 0]
add = [0, 0]
ans = ''
for i in range(n):
    if s[i] == ')':
        cnt[0] += 1
        if cnt[0] > cnt[1]:
            add[1] += 1
            cnt[1] += 1
    else:
        cnt[1] += 1

print('(' * add[1] + s + ')'*(cnt[1] -cnt[0]))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
s=input()
a=0 
b=0 
cnt=0
for i in range(n):
    if s[i]=="(":
        if a<b:
            cnt+=b-a
            a,b=1,0
        else:
            a+=1
    else:
        b+=1
if a<b:
    cnt+=b-a
s=(cnt*"("+s)
print(s+")"*(len(s)-s.count("(")))

Failing test case input:
6
))(()(

Expected output:
(())(()())


Actual output:
(())(()()))


Fixed submission (verdict: Accepted):
n=int(input())
s=input()
a=0 
b=0 
cnt=0
for i in range(n):
    if s[i]=="(":
        if a<b:
            cnt+=b-a
            a,b=1,0
        else:
            a+=1
    else:
        b+=1
if a<b:
    cnt+=b-a
s=(cnt*"("+s)
a=s.count("(")
b=len(s)-a
print(s+")"*(a-b))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
s=list(input())

i=0
cnt=0
while(i<n):
    if s[n-1-i]=='(':
        cnt+=1
        i+=1
        continue
    else:
        for j in range(cnt):
            s.append(')')
        break

a=s.count('(')
b=s.count(')')
if a<b and s[-1]==')':
    c=b-a
    for i in range(c):
        s.insert(0,'(')

    
print(''.join(s))

Failing test case input:
6
)((()(

Expected output:
()((()()))


Actual output:
)((()()


Fixed submission (verdict: Accepted):
n=int(input())
s=list(input())
cnt=0
right=0
left=0
for i in range(len(s)):
    if s[i] == '(':
        cnt+=1
    else:
        cnt-=1
    if cnt < 0:
        right+=1
        cnt=0
if cnt>0:
    left=cnt
for i in range(right):
    s.insert(0,'(')
for i in range(left):
    s.append(')')
print(''.join(s))
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
readline = sys.stdin.readline

def main():
    N = int(readline())
    S = readline().rstrip()
    res = S[:]

    if '(' in S:
        idx = S.index('(')
        cnt = 1
        for s in S[idx+1:]:
            if s == '(':
                cnt += 1
            else:
                cnt -= 1
        res += ')' * max(cnt, 0)
    
    S = res[::-1]
    idx = S.index(')')
    cnt = 1
    for s in S[idx+1:]:
        if s == ')':
            cnt += 1
        else:
            cnt -= 1
    res = '(' * max(cnt, 0) + res

    print(res)

if __name__ == '__main__':
    main()

Failing test case input:
8
()))())(

Expected output:
(((()))())()


Actual output:
(((()))())(


Fixed submission (verdict: Accepted):
import sys
readline = sys.stdin.readline

def main():
    N = int(readline())
    S = readline().rstrip()
    res = S[:]

    pls_left = 0
    pointer = 0
    for s in S:
        if s == '(':
            pointer += 1
        else:
            if pointer == 0:
                pls_left += 1
            else:
                pointer -= 1

    pls_right = pointer
    res = '(' * pls_left + S
    res += ')' * pls_right

    print(res)

if __name__ == '__main__':
    main()

Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
s = input()
c1 = 0
c2 = 0
c = 0
for i in s:
    if i == '(':
        c1 += 1
    else:
        c2 += 1
        if c1 < c2:
            c += 1
print('('*c + s + ')'*(c+c1-c2))

Failing test case input:
8
)((()))(

Expected output:
()((()))()


Actual output:
(()((()))())


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
c1 = 0
c2 = 0
c = 0
for i in s:
    if i == '(':
        c1 += 1
    else:
        c2 += 1
        if c1+c< c2:
            c += 1
print('('*c + s + ')'*(c+c1-c2))
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    N = int(input())
    S = list(input())
    answer = ""
    cnt = 0
    for s in S:
        if s == "(":
            cnt += 1
            answer += "("
        else:
            if cnt > 0:
                answer += ")"
                cnt -= 1
            else:
                answer += "()"
    if cnt > 0:
        answer += ")" * cnt
    print(answer)


if __name__ == "__main__":
    main()


Failing test case input:
8
()))())(

Expected output:
(((()))())()


Actual output:
()()()()()()


Fixed submission (verdict: Accepted):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    N = int(input())
    S = list(input())
    answer = ""
    right = 0
    left = 0
    for s in S:
        if s == "(":
            if right > 0:
                answer = "(" * right + answer
                answer += ")" * right
                right = 0
            left += 1
            answer += "("
        else:
            if left > 0:
                answer += ")"
                left -= 1
            else:
                right += 1
    if left > 0:
        answer += ")" * left
    if right > 0:
        answer = "(" * right + answer + ")" * right
    print(answer)


if __name__ == "__main__":
    main()

Problem: p03696
Buggy submission (Python (3.8.2), verdict: Wrong Answer):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

def main():
    N = int(input())
    T = input()
    S = []
    for s in T:
        if s == '(':
            S.append(1)
        else:
            S.append(-1)
    S = list(accumulate(S))
    
    minS = min(S)
    for i in range(N):
        S[i] -= minS
    print('('*(-minS) + T + ')'*S[-1])
    

if __name__ == "__main__":
    main()


Failing test case input:
8
((())(()

Expected output:
((())(()))


Actual output:
((())(())


Fixed submission (verdict: Accepted):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

def main():
    N = int(input())
    T = input()
    S = []
    for s in T:
        if s == '(':
            S.append(1)
        else:
            S.append(-1)
    S = list(accumulate(S))
    
    minS = min(S)
    if minS < 0:
        for i in range(N):
            S[i] -= minS
    print('('*max(0,-minS) + T + ')'*S[-1])

if __name__ == "__main__":
    main()
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def insertion(S):
    Sc = S
    Scc = Sc.replace("()", "")
    while Scc != Sc:
        Sc = Scc
        Scc = Scc.replace("()", "")

    
    print(Sc)
    if len(Sc) == 0:
        Sl, Sr = "", ""
    elif Sc[0] != Sc[-1]:
        pos = Sc.find(")(")
        Sl = insertion(Sc[:pos+1])[0]
        Sr = insertion(Sc[pos+1:])[2]
    elif Sc[0] == '(':
        Sl = ""
        Sr = ')' * len(Sc)
    else:
        Sl = '(' * len(Sc)
        Sr = ""

    return Sl, S, Sr

N = int(input())
s = insertion(input())
print(s[0]+s[1]+s[2])


Failing test case input:
8
)(((()))

Expected output:
()(((())))


Actual output:
)(
)
(
()(((())))


Fixed submission (verdict: Accepted):
def insertion(S):
    Sc = S
    Scc = Sc.replace("()", "")
    while Scc != Sc:
        Sc = Scc
        Scc = Scc.replace("()", "")

    
    
    if len(Sc) == 0:
        Sl, Sr = "", ""
    elif Sc[0] != Sc[-1]:
        pos = Sc.find(")(")
        Sl = insertion(Sc[:pos+1])[0]
        Sr = insertion(Sc[pos+1:])[2]
    elif Sc[0] == '(':
        Sl = ""
        Sr = ')' * len(Sc)
    else:
        Sl = '(' * len(Sc)
        Sr = ""

    return Sl, S, Sr

N = int(input())
s = insertion(input())
print(s[0]+s[1]+s[2])

Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):





























n = int(input())
s = list(input())
d = []
co, cc = 0, 0
for ch in s:
    if ch == '(':
        co += 1
    elif ch == ')':
        cc += 1
    d.append(co - cc)


x = min(d)

str_left = '(' * (-x)
str_right = ')' * (d[-1] - x)

print("{}{}{}".format(str_left, "".join(s), str_right))

Failing test case input:
8
((((()))

Expected output:
((((()))))


Actual output:
((((())))


Fixed submission (verdict: Accepted):





























n = int(input())
s = list(input())


















co, cc = 0, 0
for ch in s:
    if ch == '(':
        co += 1
    if ch == ')':
        if co > 0:
            co -= 1
        else:
            cc += 1

ans = "(" * cc + "".join(s) + ")" * co
print(ans)

Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
S = input()
countleft = 0
countright = 0
for i in range(0, N):
	if S[i] == '(':
		countleft += 1
	if S[i] == ')':
		countright += 1
ans = S
for i in range(0, countleft):
	ans = ans + ')'
for i in range(0, countright):
	ans = '(' + ans
print(ans)

Failing test case input:
8
(()((()(

Expected output:
(()((()())))


Actual output:
(((()((()())))))


Fixed submission (verdict: Accepted):
N = int(input())
S = input()
addleft = 0
countleft = 0
for i in range(0, N):
	if S[i] == '(':
		countleft += 1
	if S[i] == ')':
		if countleft == 0:
			addleft += 1
		else:
			countleft -= 1
for i in range(0, addleft):
	S = '(' + S
for i in range(0, countleft):
	S = S + ')'
print(S)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input().rstrip()
res = ''
remaining_l = 0
remaining_r = 0
left = 0
right = 0
index = 0
while index < n:
    while index < n and s[index] == '(':
        left += 1
        index += 1
    while index < n and s[index] == ')':
        right += 1
        index += 1
    if left > right:
        remaining_r += left - right
    elif left < right:
        remaining_l += right - left
    res += '(' * left + ')' * right
    left = right = 0
res = '(' * remaining_l + res + ')' * remaining_r
print(res)

Failing test case input:
6
(()())

Expected output:
(()())


Actual output:
((()()))


Fixed submission (verdict: Accepted):
n = int(input())
s = input().rstrip()
remaining_l = 0
remaining_r = 0
index = 0
while index < n:
    left = right = 0
    while index < n and s[index] == '(':
        left += 1
        index += 1
    while index < n and s[index] == ')':
        right += 1
        index += 1
    if left > right:
        remaining_r += left - right
    elif left < right:
        k = right - left
        if remaining_r <= k:
            remaining_l += k - remaining_r
            remaining_r = 0
        else:
            remaining_r -= k
print('(' * remaining_l + s + ')' * remaining_r)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = input()

cnt = 0
for x in input():
  if x == "(":
    cnt += 1
  else:
    cnt -= 1
    if cnt < 0:
      cnt += 1
      print("(", end="")
  print(x, end="")
for _ in range(cnt):
  print(")", end="")
print()



Failing test case input:
8
)(())())

Expected output:
(()(())())


Actual output:
()(())()()


Fixed submission (verdict: Accepted):
N = input()

cnt = 0
s = ""
for x in input():
  if x == "(":
    cnt += 1
  else:
    cnt -= 1
    if cnt < 0:
      cnt += 1
      s = "(" + s
  s += x
for _ in range(cnt):
  s += ")"
print(s)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
x=int(input())
y=input()
A=[]
for i in range(0,x):
  if y[i]=='(':
    A.append(1)
  elif y[1]==")":
    A.append(-1)

st=0

s=0
for i in A:

  if s>0:
    s+=i
  elif s<=0 and i==1:
    s=1
  elif s<=0 and i==-1:
    st+=1
    s+=i
out=''
for i in range(0,st):
  out+='('
for j in A:
  if j==1:
    out+='('
  elif j==-1:
    out+=')'
for i in range(0,s):
  out+=')'
print(out)


Failing test case input:
8
((())(()

Expected output:
((())(()))


Actual output:
((((()))))


Fixed submission (verdict: Accepted):
x=int(input())
y=input()
A=[]
for i in range(0,x):
  if y[i]=='(':
    A.append(1)
  elif y[i]==")":
    A.append(-1)

st=0

s=0
for i in A:
  if s==0 and i==-1:
    st+=1
  else:
    s+=i

out=''
for i in range(0,st):
  out+='('
for j in A:
  if j==1:
    out+='('
  elif j==-1:
    out+=')'
for i in range(0,s):
  out+=')'
print(out)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()
s = 0
mi = 0
for i in range(N):
    s += 1 if S[i] == "(" else -1
    mi = min(mi, s)
if s > 0:
    print(-mi * "(" + S + ")" * s)
else:
    print("(" * max(-s, -mi) + S + ")" * (max(-s, -mi) + s))


Failing test case input:
6
)(((((

Expected output:
()((((()))))


Actual output:
()((((())))


Fixed submission (verdict: Accepted):
N = int(input())
S = input()
s = 0
mi = 0
for i in range(N):
    s += 1 if S[i] == "(" else -1
    mi = min(mi, s)
if s > 0:
    print(-mi * "(" + S + ")" * (s-mi))
else:
    print("(" * max(-s, -mi) + S + ")" * (max(-s, -mi) + s))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()
bf = []
af = []
st = ['*']

for c in s:
    if (st[-1] == '(') and (c == ')'):

        st.append(c)
        af.pop()
        continue
    else:
        st.append(c)        
    if c ==  ')':
        bf.append('(')
    else:
        af.append(')')
ans = ''.join(bf) + ''.join(st[1:]) + ''.join(af)
print(ans)



Failing test case input:
8
))(())()

Expected output:
(())(())()


Actual output:
((())(())())


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
bf = []
af = []
st = ['*']

for c in s:
    pre = st[-1]
    st.append(c)    

    if c ==  ')':
        if (pre == '(') :
            st.pop()
            st.pop()
            af.pop()
            continue
        bf.append('(')
    else:
        af.append(')')
ans = ''.join(bf) + s + ''.join(af)
print(ans)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = list(input())
ans = (S)

cnt = 0
for s in S:
    if s == '(':
        cnt -= 1
    else :
        cnt += 1
if cnt>0:
    ans = ['('] * cnt + ans
               
S = ans
cnt = 0
for s in S:
    if s == ')':
        cnt += 1
    else :
        cnt -= 1
if cnt<0:
    ans =  ans + [')'] * (-cnt) 

print(''.join(ans))


Failing test case input:
6
))((((

Expected output:
(())(((())))


Actual output:
))(((())


Fixed submission (verdict: Accepted):
N = int(input())
S = list(input())
ans = (S)

cnt = 0
for i,s in enumerate(S):
    if cnt < 0:
        ans = ['('] + ans
        cnt = 0
    if s == '(':
        cnt += 1
    else:
        cnt -= 1
if cnt> 0:
    ans = ans + [')'] * cnt
elif cnt < 0:
    ans = ['('] * (-cnt) + ans
print(''.join(ans))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()

T = [+1 if s=='(' else -1 for s in S]

from itertools import accumulate
Tcumsum = list(accumulate(T))

i = - min(Tcumsum)

print('(' * i + S + ')' * (Tcumsum[-1]+i))

Failing test case input:
8
((((()))

Expected output:
((((()))))


Actual output:
((((())))


Fixed submission (verdict: Accepted):
N = int(input())
S = input()

T = [+1 if s=='(' else -1 for s in S]

from itertools import accumulate
Tcumsum = list(accumulate(T))

i = max(0, - min(Tcumsum))

print('(' * i + S + ')' * (Tcumsum[-1]+i))
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = list(input())

mae = S.count("(")
ushiro = N - mae

p_sum = [0] * N

if S[0] == "(":
	p_sum[0] = 1
else:
	p_sum[0] = -1

for i in range(1, N):
	if S[i] == "(":
		p_sum[i] = p_sum[i - 1] + 1
	else:
		p_sum[i] = p_sum[i - 1] - 1

def Good(x):
	if min(x) >= 0:
		return True
	else:
		return False

if not Good(p_sum):
	S = ["("] * (- min(p_sum)) + S + [")"] * (mae - ushiro - min(p_sum))

print("".join(S))

Failing test case input:
8
()((()((

Expected output:
()((()(())))


Actual output:
()((()((


Fixed submission (verdict: Accepted):
N = int(input())
S = list(input())

mae = S.count("(")
ushiro = N - mae

p_sum = [0] * N

if S[0] == "(":
	p_sum[0] = 1
else:
	p_sum[0] = -1

for i in range(1, N):
	if S[i] == "(":
		p_sum[i] = p_sum[i - 1] + 1
	else:
		p_sum[i] = p_sum[i - 1] - 1

def Good(x):
	if min(x) >= 0 and mae == ushiro:
		return True
	else:
		return False

if not Good(p_sum):
	S = ["("] * (- min(0, min(p_sum))) + S + [")"] * (mae - ushiro - min(0, min(p_sum)))

print("".join(S))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()
cnt = [0, 0]
ans = ''
for i in range(n):
    if s[i] == ')':
        cnt[0] += 1
    else:
        cnt[1] += 1
print('(' * cnt[0] + s + ')'*cnt[1])


Failing test case input:
8
()))()((

Expected output:
((()))()(())


Actual output:
((((()))()(())))


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
cnt = [0, 0]
add = [0, 0]
ans = ''
for i in range(n):
    if s[i] == ')':
        cnt[0] += 1
        if cnt[0] > cnt[1]:
            add[1] += 1
            cnt[1] += 1
    else:
        cnt[1] += 1

print('(' * add[1] + s + ')'*(cnt[1] -cnt[0]))

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def main():
    from collections import deque
    n = int(input())
    s = input()
    t = s.count("()")
    c_l = 0
    c_r = 0

    for i in s:
        if i == ")":
            c_l += 1
        else:
            c_r += 1

    c_l -= t
    c_r -= t
    print(c_l * "(" + s + ")" * c_r)


if __name__ == '__main__':
    main()


Failing test case input:
8
)))((())

Expected output:
((()))((()))


Actual output:
(((()))((())))


Fixed submission (verdict: Accepted):
def main():
    n = int(input())
    s = input()

    l, r = 0, 0

    for i in s:
        if i == ")":
            if l:
                l -= 1
            else:
                r += 1
        else:
            l += 1

    print(r * "(" + s + ")" * l)


if __name__ == '__main__':
    main()
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
S=input()
sl=list(S)
change=[0]
for i in range(1,N):
  if (sl[i-1]==')')and(sl[i]=='('):
    change.append(i)

change.append(N)
r_add=0
l_add=0
for i in range(len(change)-1):
  A=sl[change[i]:change[i+1]]
  r=0
  l=0
  for a in A:
    if a=='(':
      l+=1
    else:
      r+=1
      
  if r==l:
    continue
  elif r>l:
    l_add+=r-l
    r_add=max(0,r_add-(r-l))
  else:
    r_add+=l-r
      
print('('*l_add+S+')'*r_add)

Failing test case input:
6
(()())

Expected output:
(()())


Actual output:
((()())


Fixed submission (verdict: Accepted):
N=int(input())
S=input()
sl=list(S)
change=[0]
for i in range(1,N):
  if (sl[i-1]==')')and(sl[i]=='('):
    change.append(i)

change.append(N)
r_add=0
l_add=0
for i in range(len(change)-1):
  A=sl[change[i]:change[i+1]]
  r=0
  l=0
  for a in A:
    if a=='(':
      l+=1
    else:
      r+=1
      
  if r==l:
    continue
  elif r>l:
    if r_add>0:
      if r_add>=r-l:
        r_add-=r-l
      else:
        l_add+=r-l-r_add
        r_add=0
        
    else:
      l_add+=r-l
      
  else:
    r_add+=l-r
      
print('('*l_add+S+')'*r_add)

Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
def main():
    n = int(input())
    S = input()
    a, b = 0, 0
    m = float('inf')
    for s in S:
        if s == '(':
            a += 1
        else:
            b += 1
        m = min(m, a - b)
    print(''.join(['(' * (-m), S, ')' * (a - b - m)]))


if __name__ == '__main__':
    main()


Failing test case input:
8
(()((()(

Expected output:
(()((()())))


Actual output:
(()((()()))


Fixed submission (verdict: Accepted):
def main():
    n = int(input())
    S = input()
    a, b = 0, 0
    aa = 0
    m = 0
    for s in S:
        if aa > 0 and s == ')':
            m += 1
            aa -= 1
        if s == '(':
            a += 1
            aa += 1
        else:
            b += 1
    print(''.join(['(' * (b - m), S, ')' * (a - m)]))

if __name__ == '__main__':
    main()

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
s=input()
ss=s
x=0
while x<len(ss)-1:
    if ss[x]=='(' and ss[x+1]==')':
        ss=ss[:x]+ss[x+2:]
        x=0
    x+=1
left=0
right=0
for i in ss:
    if i=='(':
        right+=1
    elif i==')':
        left+=1
q='('*left
w=')'*right
print(q+s+w)


Failing test case input:
8
()(()())

Expected output:
()(()())


Actual output:
(()(()()))


Fixed submission (verdict: Accepted):
n=int(input())
s=input()
ss=s
x=0
while x<len(ss)-1:
    if ss[x]=='(' and ss[x+1]==')':
        ss=ss[:x]+ss[x+2:]
        x=0
    else:
        x+=1
left=0
right=0
for i in ss:
    if i=='(':
        right+=1
    elif i==')':
        left+=1
q='('*left
w=')'*right
print(q+s+w)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input().rstrip()
res = ''
remaining_l = 0
remaining_r = 0
left = 0
right = 0
index = 0
while index < n:
    while index < n and s[index] == '(':
        left += 1
        index += 1
    while index < n and s[index] == ')':
        right += 1
        index += 1
    if left > right:
        remaining_r += left - right
    elif left < right:
        k = right - left
        if remaining_r <= k:
            remaining_r = 0
            remaining_l += k - remaining_r
        else:
            remaining_r -= k
    res += '(' * left + ')' * right
    left = right = 0
res = '(' * remaining_l + res + ')' * remaining_r
print(res)

Failing test case input:
6
(()())

Expected output:
(()())


Actual output:
((()())


Fixed submission (verdict: Accepted):
n = int(input())
s = input().rstrip()
remaining_l = 0
remaining_r = 0
index = 0
while index < n:
    left = right = 0
    while index < n and s[index] == '(':
        left += 1
        index += 1
    while index < n and s[index] == ')':
        right += 1
        index += 1
    if left > right:
        remaining_r += left - right
    elif left < right:
        k = right - left
        if remaining_r <= k:
            remaining_l += k - remaining_r
            remaining_r = 0
        else:
            remaining_r -= k
print('(' * remaining_l + s + ')' * remaining_r)

Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from collections import deque
import sys
input = sys.stdin.readline
n = int(input()) 

s = list(input())
que = deque()
done = deque()
i = 0
while i < n:
    t = s[i]
    if t == '(':
        if i + 1 < n and s[i + 1] == ')':
            que.append(t)
            i += 1
        else:
            c = i
            while i + 1 < n and s[i + 1] == '(':
                i += 1
            i += 1
            que.append('(' * (i - c))
            que.append(')' * (i - c))
    elif que and que[-1] == '(':
        que.append(t)
        i += 1
        
    else:
        que.appendleft('(')
        que.append(t)
        i += 1

print(''.join(que))


Failing test case input:
6
())(()

Expected output:
(())(())


Actual output:
((())(()))


Fixed submission (verdict: Accepted):
from collections import deque
import sys
input = sys.stdin.readline
n = int(input()) 

s = list(input())
que = deque()
stack = []

for i in range(n):
    t = s[i]
    if t == '(':
        stack.append(t)
        que.append(t)
    else: 
        if stack:
            if stack[-1] == '(':
                stack.pop()
                que.append(')')
            else:
                stack.append(')')
        else:
            que.appendleft('(')
            que.append(')')

print(''.join(que) + ')' * len(stack))