ZhenlongDai/ACPR huggingface dataset:
Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = list(map(int,input().split()))

nm = 10 ** 10
nv = -1
for i in range(n):
    if a[i] > nv:
        nv = a[i]
        continue
    
    if a[i] < nm:
        nm = a[i]

print(nv-nm)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):
n = int(input())
a = list(map(int,input().split()))

nm = a[0]
nv = a[0]
for i in range(n):
    if a[i] > nv:
        nv = a[i]
        continue
    
    if a[i] < nm:
        nm = a[i]

print(nv-nm)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
A=list(map(int,input().split()))
ans=0

for i in range(0,N):
  for j in range(i,N):
    ans = max(abs(i-j),ans)
    
print(ans)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):
N=int(input())
A=list(map(int,input().split()))
ans=0

for i in range(0,N):
  for j in range(i,N):
    ans = max(abs(A[i]-A[j]),ans)
    
print(ans)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
if __name__ == '__main__':
    s = input()
    a = [int(i) for i in s.split()]
    mina = min(a)
    maxa = max(a)
    print(maxa - mina)


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):

if __name__ == '__main__':
    n = input()
    s = input()
    a = [int(i) for i in s.split()]
    mina = min(a)
    maxa = max(a)
    print(maxa - mina)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = list(map(int , input().split()))
ans = 0
prev = a[0]
for ai in a:
    ans = max(ans, abs(prev - ai))
    prev = ai
print(ans)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):
n = int(input())
a = list(map(int , input().split()))
ans = 0
for ai in a:
    for bi in a:
        ans = max(ans, abs(bi - ai))
print(ans)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def main():
    n = int(input())
    nums = input().split()
    print(nums)
    for i in range(len(nums)):
        nums[i] = int(nums[i])
    print(nums)
    max_diff = 0
    for i in range(len(nums)):
        for j in range(i, len(nums)):
            diff = abs(nums[i] - nums[j])
            if diff > max_diff:
                max_diff = diff
    print(max_diff)

main()


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
['1', '4', '6', '3']
[1, 4, 6, 3]
5


Fixed submission (verdict: Accepted):
def main():
    n = int(input())
    nums = input().split()
    for i in range(len(nums)):
        nums[i] = int(nums[i])
    max_diff = 0
    for i in range(len(nums)):
        for j in range(i, len(nums)):
            diff = abs(nums[i] - nums[j])
            if diff > max_diff:
                max_diff = diff
    print(max_diff)

main()
Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = list(map(int, input().split()))
print(max(N)-min(N))

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
N = int(input())
A = list(map(int, input().split()))
print(max(A)-min(A))

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
l = sorted(list(map(int, input().split())))
ans = l[-1] - l[0]
print(ans)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
input()
a = sorted(map(int,input().split()))
print(a[-1] - a[0])

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


def solve():
    N = input()
    A = list(map(int, input().split()))
    min_a = 0
    max_a = float("inf")
    for a in A:
        min_a = min(min_a, a)
        max_a = max(max_a, a)
    print(max_a - min_a)

if __name__ == "__main__":
    solve()


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
inf


Fixed submission (verdict: Accepted):


def solve():
    N = input()
    A = list(map(int, input().split()))
    min_a = float("inf")
    max_a = 0
    for a in A:
        min_a = min(min_a, a)
        max_a = max(max_a, a)
    print(max_a - min_a)

if __name__ == "__main__":
    solve()

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=input()
a=input().split()
a=[int(x) for x in a]
print(a)
t=0
for i in range(len(a)-1):
    for j in range(i+1, len(a)):
        if abs(a[i]-a[j])>t:
            t=abs(a[i]-a[j])
        
print(t)






Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
[1, 4, 6, 3]
5


Fixed submission (verdict: Accepted):
N=input()
a=input().split()
a=[int(x) for x in a]

t=0
for i in range(len(a)-1):
    for j in range(i+1, len(a)):
        if abs(a[i]-a[j])>t:
            t=abs(a[i]-a[j])
        
print(t)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
A=list(map(int,input().split()))
A.sort()
print(A)
print(str(A[n-1]-A[0]))


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
[1, 3, 4, 6]
5


Fixed submission (verdict: Accepted):
n=int(input())
A=list(map(int,input().split()))
A.sort()

print(str(A[n-1]-A[0]))

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

A=list(map(int, input().split()))

A_s = set(A)
A.sort()
A.reverse()

a1 = A[0]
a2 = A[1]


print(a1-a2)


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
2


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

A=list(map(int, input().split()))

A_s = set(A)
A.sort()
A.reverse()

a1 = A[0]
a2 = A[-1]


print(a1-a2)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = list(map(int, input().split()))

for i in a:
    if i < 0:
        i *= -1

a.sort()
print(a[0]-a[-1])


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
-5


Fixed submission (verdict: Accepted):
n = int(input())
a = list(map(int, input().split()))

for i in range(n):
    if a[i] < 0:
        a[i] *= -1

a.sort()
print(a[-1]-a[0])
Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = list(map(float, input().split()))

print(int(abs(max(N) - min(N))))


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
_, N = input(), list(map(float, input().split()))

print(int(abs(max(N) - min(N))))

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = input()
list = sorted(map(int, input().split()))
print(list[-1] - list[-2])

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
2


Fixed submission (verdict: Accepted):
N = input()
list = sorted(map(int, input().split()))
print(list[-1] - list[0])

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
a=input()
a=sorted(map(int,input().split()))
print(a[-1]-a[1])


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):
a=input()
a=sorted(map(int,input().split()))
print(a[-1]-a[0])

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = list(map(int,input().split()))
maxi = max(n)
mini = min(n)
print(maxi - mini)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
n = int(input())
l= list(map(int,input().split()))
maxi = max(l)
mini = min(l)
print(maxi - mini)

Problem: p03308
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
a = list(map(int, input().split()))

mx_diff = 0
for i in range(1, N):
    if abs(a[i-1]-a[i]) > mx_diff:
        mx_diff = abs(a[i-1]-a[i])
print(mx_diff)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):
N = int(input())
a = list(map(int, input().split()))

mx_a = 1
mn_a = 10**9
for i in range(N):
    if a[i] > mx_a:
        mx_a = a[i]
    if a[i] < mn_a:
        mn_a = a[i]
print(mx_a-mn_a)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
a=list(map(int,input().split()))
print(a)   
a_max=max(a)
a_min=min(a)
print(a_max-a_min)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
[1, 4, 6, 3]
5


Fixed submission (verdict: Accepted):
N=int(input())
a=list(map(int,input().split())) 
a_max=max(a)
a_min=min(a)
print(a_max-a_min)

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

N = int(input())
A = [int(i) for i in range(N)]

max_ = max(A)
min_ = min(A)
print(max_ - min_)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):

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

max_ = max(A)
min_ = min(A)
print(max_ - min_)
Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input().strip())
arry = list(map(int,input().strip().split()))

min = arry[1]
max = arry[1]

print  (len(arry))

for i in range(len(arry)):
  if arry[i] < min:
    min = arry[i]
  if arry[i] > max:
    max = arry[i]

print (max-min)


Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
4
5


Fixed submission (verdict: Accepted):
n = int(input().strip())
arry = list(map(int,input().strip().split()))

min = arry[1]
max = arry[1]


for i in range(len(arry)):
  if arry[i] < min:
    min = arry[i]
  if arry[i] > max:
    max = arry[i]

print (max-min)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = input()
aryA = input().split(" ")

N = int(N)
aryA = [int(s) for s in aryA]
ans = []
for x in range(len(aryA) - 1):
    ans.append(abs(aryA[x+1] - aryA[x]))

print(max(ans))

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):
N = input()
aryA = input().split(" ")

N = int(N)
aryA = [int(s) for s in aryA]
ans = []
for x in range(len(aryA) - 1):
    for y in range(len(aryA)):
        ans.append(abs(aryA[x] - aryA[y]))

print(max(ans))

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = sorted(map(int,input().split()))
print(s[-1]-s[0])

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
input()
s = sorted(map(int,input().split()))
print(s[-1]-s[0])

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
a = list(map(int, input().split()))
max = 0
for i in range(len(a)):
    for j in range(i+1, len(a)):
        print(i,j)
        if abs(a[i] - a[j]) > max:
            max = abs(a[i] - a[j])
print(max)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0 1
0 2
0 3
1 2
1 3
2 3
5


Fixed submission (verdict: Accepted):
N = int(input())
a = list(map(int, input().split()))
max = 0
for i in range(len(a)):
    for j in range(i+1, len(a)):
        if abs(a[i] - a[j]) > max:
            max = abs(a[i] - a[j])
print(max)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
A = list(map(int,input().split()))
maxi = 0
for i in range(N-1):
  maxi = max(maxi,A[i]-A[i+1])
print(maxi)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):
N = int(input())
A = list(map(int,input().split()))
maxi = 0
mini = float("inf")
for n in A:
  maxi = max(maxi,n)
  mini = min(mini,n)
print(maxi-mini)

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


 
def main():
	N = int(input())
	if N % 2 == 0:
		print(int(N))
	else:
		print(int(N*2))
 
if __name__ == '__main__':
	main()

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
4


Fixed submission (verdict: Accepted):


 
def main():
	N = int(input())
	A = list(map(int, input().split()))
	print(max(A)-min(A))

if __name__ == '__main__':
	main()
Problem: p03308
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
li=list(map(int,input().split()))
print(max(li)-min(li))

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
n=int(input())
li=list(map(int,input().split()))
print(max(li)-min(li))

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


def main():
    
    N = int(input())
    lists = list(map(int, input().split()))

    
    max_element = 0

    
    for i in range(1, N):
        diff = lists[i] - lists[i-1]
        if diff > max_element:
            max_element = diff

    print(max_element)


if __name__ == '__main__':
    main()

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):


def main():
    
    N = int(input())
    lists = list(map(int, input().split()))

    
    max_element = 0
    min_element = lists[0]

    
    for i in range(N):
        if lists[i] > max_element:
            max_element = lists[i]
        if lists[i] < min_element:
            min_element = lists[i]

    print('{}'.format(max_element - min_element))

if __name__ == '__main__':
    main()

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
a=input()
a=sorted(map(int,input().split()))
print(a[-1]-a[1])



Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
3


Fixed submission (verdict: Accepted):
a=input()
a=sorted(map(int,input().split()))
print(a[-1]-a[0])

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
value = []
for part in input().split():
    value.append(int(part))

print(max(value) - min(value))

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
N = int(input())
    
value = []
for part in input().split():
    value.append(int(part))
    
print(max(value) - min(value))

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
input;n=list(map(int,input().split()))
print(max(n) - min(n))

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
input();n=list(map(int,input().split()))
print(max(n) - min(n))

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=input()
a=input().split()
a=[int(x) for x in a]
print(a)
t=0
for i in range(len(a)):
    for j in range(i+1, len(a)):
        if abs(a[i]-a[j])>t:
            t=abs(a[i]-a[j])
        
print(t)






Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
[1, 4, 6, 3]
5


Fixed submission (verdict: Accepted):
N=input()
a=input().split()
a=[int(x) for x in a]

t=0
for i in range(len(a)-1):
    for j in range(i+1, len(a)):
        if abs(a[i]-a[j])>t:
            t=abs(a[i]-a[j])
        
print(t)

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
A=list(map(int, input().split()))
max(A)-min(A)

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:

Fixed submission (verdict: Accepted):
N=int(input())
A=list(map(int, input().split()))
print(max(A)-min(A))
Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
Alist = [int(i) for i in input().split()]

Alist.reverse()
print(Alist[0]-Alist[N-1])

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
Alist = [int(i) for i in input().split()]

Alist.sort()
print(Alist[N-1]-Alist[0])

Problem: p03308
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
ln=[int(n) for n in map(int,input().split())]
print(max(ln)-max(ln))

Failing test case input:
4
1 4 6 3

Expected output:
5

Actual output:
0


Fixed submission (verdict: Accepted):
n=int(input())
ln=[int(n) for n in map(int,input().split())]
print(max(ln)-min(ln))
Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n=int(input())
s=input()
x=[]
y=0
for i in range(n):
    if s[i]=="(":
        x.append("(")
        y+=1
    elif y>0:
        x.append(")")
        y-=1
    else:
        x.append(")")
        x=["("]+x
print("".join(x))

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

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


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


Fixed submission (verdict: Accepted):
n=int(input())
s=input()
x=[]
y=0
for i in range(n):
    if s[i]=="(":
        x.append("(")
        y+=1
    elif y>0:
        x.append(")")
        y-=1
    else:
        x.append(")")
        x=["("]+x
x=x+[")"]*y
print("".join(x))

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

def main():
    n = stdin.readline()
    S = stdin.readline()
    cnt = 0
    m = 0
    for s in S:
        if s == "(":
            cnt += 1
        else:
            cnt -= 1
        
        if cnt < 0:
            m += 1
            cnt += 1
    
    print("("*m+S+")"*cnt)
    
if __name__ == "__main__":
    main()



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

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


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



Fixed submission (verdict: Accepted):
def main():
    n = input()
    S = input()
    cnt = 0
    m = 0
    for s in S:
        if s == '(':
            cnt += 1
        else:
            cnt -= 1
        
        if cnt < 0:
            m += 1
            cnt += 1
            
    ans = "("*m+S+")"*cnt
    print(ans)
    
if __name__ == "__main__":
    main()

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

num = 0
l = 0
r = 0
for i in range(n):
    if s[i] == ")":
        num += 1
    else:
        num -= 1
    if num == 1:
        l += 1
        num = 0

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

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

Failing test case input:
3
(()

Expected output:
(())


Actual output:
(()


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

num = 0
lmax = 0
rmax = 0
for i in range(n):
    if s[i] == ")":
        num += 1
    else:
        num -= 1
    lmax = max(lmax, num)

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

print("("*lmax + s + ")"*rmax)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()

num = 0
l = 0
r = 0
for i in range(n):
    if s[i] == ")":
        num += 1
    else:
        num -= 1
    if num == -1:
        l += 1
        num = 0

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

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

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

Expected output:
(())()()


Actual output:
))()())


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

num = 0
lmax = 0
rmax = 0
for i in range(n):
    if s[i] == ")":
        num += 1
    else:
        num -= 1
    lmax = max(lmax, num)

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

print("("*lmax + s + ")"*rmax)

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

S = input()

while True:
    pre = S
    S = re.sub(r'\(((L|R)*?)\)', r'L\1R', S)
    if pre == S:
        break

S = '(' * S.count(')') + S + ')' * S.count('(')
S = S.replace('L', '(')
S = S.replace('R', ')')
print(S)

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

Expected output:
(((())))


Actual output:
8


Fixed submission (verdict: Accepted):
import re

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

while True:
    pre = S
    S = re.sub(r'\(((L|R)*?)\)', r'L\1R', S)
    if pre == S:
        break

S = '(' * S.count(')') + S + ')' * S.count('(')
S = S.replace('L', '(')
S = S.replace('R', ')')
print(S)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = input()
s = input()
s1 = s
s2 = ''
while s1 != s2: 
    s2 = s1
    s1 = s1.replace('()','')
print(s1)
s2 = s1.replace('(','')

print(len(s2)*'(' + s + (len(s1)-len(s2))*')')


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

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


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


Fixed submission (verdict: Accepted):
N = input()
s = input()
s1 = s
s2 = ''
while s1 != s2: 
    s2 = s1
    s1 = s1.replace('()','')
s2 = s1.replace('(','')

print(len(s2)*'(' + s + (len(s1)-len(s2))*')')

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

n=int(input())
s=input()
ss=s
l_cnt=0
r_cnt=0
if s.count('()')>0:
    ss=ss.replace('()','')
    l_cnt=ss.count('(')
    r_cnt=ss.count(')')
    if l_cnt>r_cnt:
        s+=')'*(l_cnt-r_cnt)
    elif l_cnt==r_cnt:
        pass
    elif l_cnt<r_cnt:
        s='('*(r_cnt-l_cnt)+s
    print(s)
else:
    l_cnt=s.count('(')
    r_cnt=s.count(')')
    s+=')'*l_cnt
    s='('*r_cnt+s
    print(s)

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

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


Actual output:
()))(()(


Fixed submission (verdict: Accepted):

n=int(input())
s=input()
ss=s
l_cnt=0
r_cnt=0
if s.count('()')>0:
    
    while ss.count('()')>0:
        ss=ss.replace('()','')
    l_cnt=ss.count('(')
    r_cnt=ss.count(')')
    s+=')'*l_cnt
    s='('*r_cnt+s

    print(s)
else:
    l_cnt=s.count('(')
    r_cnt=s.count(')')
    s+=')'*l_cnt
    s='('*r_cnt+s
    print(s)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

import math
import itertools
import statistics

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

amari = 0
plus = 0

for i in range(n):
    if s[i]==")":
        plus += 1
    else:
        if plus <= 0:
            amari +=1
        else:
            plus -= 1

for i in range(amari):
    print("(", end="")

print(s, end="")

for i in range(plus):
    print(")", end="")








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

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


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

Fixed submission (verdict: Accepted):

import math
import itertools
import statistics

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

amari = 0
plus = 0

for i in range(n):
    if s[i]=="(":
        plus += 1
    else:
        if plus <= 0:
            amari +=1
        else:
            plus -= 1

for i in range(amari):
    print("(", end="")

print(s, end="")

for i in range(plus):
    print(")", end="")

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

d = S.replace('()','')

l = d.count('(')
r = d.count(')')

z = '('*l + S + ')'*r

print(z)

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

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


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


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

d = S
while d.count('()')!=0:
	d = d.replace('()','')

l = d.count('(')
r = d.count(')')

z = '('*r + S + ')'*l

print(z)

Problem: p03696
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
from collections import deque
n = int(input())
s = input()
A = deque([])
for i in range(n):
  if s[i]=='(':
    A.append(s[i])
  elif s[i]==')':
    if len(A)!=0 and A[-1]=='(':
      A.append(s[i])
    elif len(A)==0 or A[-1]==')':
      A.appendleft("(")
      A.append(")")
cou = -1
for i in range(len(A)):
  if A[i]==')':
    cou = i
amari = len(A)-(cou+1)

for j in range(amari):
  A.append(")")
print("".join(A))

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

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


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


Fixed submission (verdict: Accepted):
from collections import deque
n = int(input())
s = input()
A = deque([])
l = 0
r = 0
for i in range(n):
  if s[i]=='(':
    A.append(s[i])
    l += 1
  else:
    if l!=0:
      A.append(s[i])
      l -=1
    else:
      A.appendleft("(")
      A.append(s[i])

for j in range(l):
  A.append(")")
  
print("".join(A))
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()

ans = []
st = 0
for c in S:
    if c == '(':
        st += 1
        ans.append('(')
    else:
        if st > 0:
            st -= 1
        else:
            ans.insert(0, '(')
        ans.append(')')
    print(*ans, sep='')

ans.extend(')' * st)

print(*ans, sep='')


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

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


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


Fixed submission (verdict: Accepted):
N = int(input())
S = input()
 
ans = []
st = 0
for c in S:
    if c == '(':
        st += 1
        ans.append('(')
    else:
        if st > 0:
            st -= 1
        else:
            ans.insert(0, '(')
        ans.append(')')
 
ans.extend(')' * st)
 
print(*ans, sep='')

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
N = input()
L = 0
R = 0
num = 0
for c in N:
    if c == '(':
        num += 1
    else:
        if num:
            num -= 1
            print(num)
        else:
            L += 1
R = num

print('('*L+N+')'*R)

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

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


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


Fixed submission (verdict: Accepted):
n = int(input())
N = input()
L = 0
R = 0
num = 0
for c in N:
    if c == '(':
        num += 1
    else:
        if num:
            num -= 1
        else:
            L += 1
R = num

print('('*L+N+')'*R)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
kakko=[0]*(int(input())+1)
S=input()
kakkoSum=0
for n,s in enumerate(S):
    if s=="(":
        kakkoSum +=1
    else:
        kakkoSum -=1
    kakko[n+1] = kakkoSum
print(kakko)
D=min(kakko)
if kakko[-1] ==0:print(S)
elif D<0:
    d=kakko[-1]
    kakkoleft=-D
    kakkoright=-D+d
    print("(" * kakkoleft + S + ")" * kakkoright)
else:
    d=kakko[-1]
    print(S + ")"* d)

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

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


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


Fixed submission (verdict: Accepted):
kakko=[0]*(int(input())+1)
S=input()
kakkoSum=0
for n,s in enumerate(S):
    if s=="(":
        kakkoSum +=1
    else:
        kakkoSum -=1
    kakko[n+1] = kakkoSum
D=min(kakko)
if D<0:
    d=kakko[-1]
    kakkoleft=-D
    kakkoright=-D+d
    print("(" * kakkoleft + S + ")" * kakkoright)
elif kakko[-1] ==0:print(S)
else:
    d=kakko[-1]
    print(S + ")"* d)
Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    s = list(input()[:-1].decode())
    ans = []
    c1 = 0
    c2 = 0
    for i in range(n):
        if s[i] == '(':
            c1 += 1
        elif s[i] == ')':
            c2 += 1

        if c1 == 0 and c2 == 1:
            ans = ['('] + ans + [')']
            c1 = c2 = 0
        elif c1 >= 1 and c2 == 1:
            ans = ans + ['('] + [')']
            c1 -= 1
            c2 = 0
    ans = ans + ['('] * (c1 - c2) + [')'] * (c1 - c2)

    print(*ans, sep='')


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()


Failing test case input:
3
(()

Expected output:
(())


Actual output:
()()


Fixed submission (verdict: Accepted):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    s = input()[:-1].decode()
    c1 = 0
    c2 = 0
    for i in range(n):
        if s[i] == '(':
            c1 += 1
        elif s[i] == ')':
            c1 -= 1
        if c1 < 0:
            c1 = 0
            c2 += 1
    print('(' * c2, s, ')' * c1, sep='')


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

left = ''
right = ''

skip_idx = []
for idx, s in enumerate(S):
    if idx in skip_idx:
        continue
    if s == ')':
        left += '('
    else:
        
        skip_flag = 0
        for n in range(idx, N):
            if S[n] == ')':
                skip_idx.append(n)
                skip_flag = 1
                break
        if not skip_flag:
            right += ')'
    

print(left+S+right)

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

Expected output:
()((()))


Actual output:
()((()


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

left = ''
right = ''

skip_idx = []
for idx, s in enumerate(S):
    if idx in skip_idx:
        continue
    if s == ')':
        left += '('
    else:
        
        skip_flag = 0
        for n in range(idx+1, N):
            if n in skip_idx:
                continue
            if S[n] == ')':
                skip_idx.append(n)
                skip_flag = 1
                break
        if not skip_flag:
            right += ')'
    

print(left+S+right)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))



N=one_int()
S=one_str()

try:
    first_start_index=S.index("(")
except:
    first_start_index=0

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



ans=S
if count >0:
    ans+= ")"*count
else:
    ans = "("*int(abs(count)) +ans

ans = first_start_index*"(" + ans

print(ans)

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

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


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


Fixed submission (verdict: Accepted):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))



N=one_int()
S=one_str()

head = 0
tail = 0

point=0
for a in S:
    if a =="(":
        point+=1
    else:
        point -=1
    if point<0:
        head += 1
        
        
        point += 1


print("("*head + S + abs(point)*")")

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque

N=int(input())
S=input()
stack= deque([])
ans=""

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

nokori=len(stack)
ans += nokori * "(" + 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):
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 = stack.pop()+ ans + ")"
    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):
n = int(input())
s = input()
r = s.count('(')
lr = 0
li = n
for i in range(n):
    if s[i] == '(':
        li = i
        break
for i in range(li, n):
    if s[i] == '(':
        lr += 1
    else:
        lr -= 1
res = ''
res += '('*max(0,-lr)
if li != n:
    res += '('*li
res = res + s + ')' * max(0, lr)
print(res)

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

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


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


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
r = 0
lr = 0
li = 0
for i in range(n):
    if s[i] == '(':
        li = i
        break
    if i==n-1:
        li = i
for i in range(li, n):
    if s[i] == '(':
        lr += 1
    else:
        if lr > 0:
            lr -= 1
        else:
            r += 1

res = '('*r
res += '('*li
res = res + s + ')' * max(0, lr)
print(res)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from itertools import groupby
N = int(input())
S = input()
ans = []

a=0
a1=0
b=0
b1=0
S1 = groupby(list(S))
for key,group in S1:
  group = list(group)
  if list(key)==['(']:
    a=len(group)
    a1+=a
  else:
    b=len(group)
    if a1==0:
      b1+=b
    else:
      a1-=b
S = "("*b1+S
S = S+")"*a1
print("".join(S))

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

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


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


Fixed submission (verdict: Accepted):
from itertools import groupby
N = int(input())
S = input()
ans = []

a=0
a1=0
b=0
b1=0
S1 = groupby(list(S))
for key,group in S1:
  group = list(group)
  if list(key)==['(']:
    a1+=len(group)
  else:
    if a1<=len(group):
      b1+=len(group)-a1
      a1 = 0
    else:
      a1 -=len(group)
S = S+")"*a1
S = "("*b1+S
print(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 = ''
    cnt = 0
    for s in S[::-1]:
        if s == ')':
            res += ')'
            cnt += 1
        else:
            if cnt > 0:
                res += '('
                cnt -= 1
            else:
                res += ')('

    res += '(' * max(cnt, 0)
    print(res[::-1])

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 (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()
left = 0
right = 0
cnt1 = 0
for i in S:
    if i == '(':
        left += 1
    else:
        right += 1
    cnt1 = max(cnt1, right - left)
cnt2 = 0
for i in S[::-1]:
    if i == ')':
        left += 1
    else:
        right += 1
    cnt2 = max(cnt2, right - left)
ans = '(' * max(0, cnt1) + S + ')' * max(0, cnt2)
print(ans)

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

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


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


Fixed submission (verdict: Accepted):
N = int(input())
S = input()
left = 0
right = 0
cnt1 = 0
for i in S:
    if i == '(':
        left += 1
    else:
        right += 1
    cnt1 = max(cnt1, right - left)
left = 0
right = 0
cnt2 = 0
for i in S[::-1]:
    if i == ')':
        left += 1
    else:
        right += 1
    cnt2 = max(cnt2, right - left)
ans = '(' * max(0, cnt1) + S + ')' * max(0, cnt2)
print(ans)

Problem: p03696
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
s = input()
t = 0
ss = 0
ee = 0

if s[0] == ')' or s[-1] == '(':
    for i in range(n):
        if s[i] == ')':
            ss += 1
        else:
            break
    st = s[::-1]
    for i in range(n):
        if st[i] == '(':
            ee += 1
        else:
            break

for i in range(n):
    if s[i] == '(':
        t += 1
    else:
        t -= 1
t -= (ee-ss)
if t >= 0:
    tail = ')'*(t)
    ans = s+tail
else:
    head = '('*(-t)
    ans = head+s
print('('*ss+ans+')'*ee)


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

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


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


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
c = 0
d = 0
for i in range(n):
    if s[i] == '(':
        c += 1
    else:
        d += 1
used_c = [True]*n
used_d = [True]*n
for i in range(n):
    if s[i] == '(':
        for j in range(i+1, n):
            if s[j] == ')' and used_c[j]:
                c -= 1
                used_c[j] = False
                break
    else:
        for j in range(i):
            if s[j] == '(' and used_d[j]:
                d -= 1
                used_d[j] = False
                break
print('('*d+s+')'*c)

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

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

    if cnt < 0:
        left = max(left, -cnt)

S = "(" * left + S

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

S = S + ")" * right

print(S)

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

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


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



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

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

        if cnt < 0:
            left = max(left, -cnt)

S = "(" * left + S

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

S = S + ")" * right

print(S)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()
lv = [0] * n

lvi = 0
for i,v in enumerate(s):
    if v == '(' :
        lvi += 1
    else:
        lvi -= 1
    lv[i] = lvi
l = min(lv)
r = -lv[-1] + l

print('('*(-l) + s + ')'*(-r))

Failing test case input:
3
(()

Expected output:
(())


Actual output:
(()


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
lv = [0] * n

lvi = 0
for i,v in enumerate(s):
    if v == '(' :
        lvi += 1
    else:
        lvi -= 1
    lv[i] = lvi
l = min(0,min(lv))
r = -lv[-1] + l

print('('*(-l) + s + ')'*(-r))

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


left = 0  
right = 0 
l_count = 0
for i in range(N):
    if S[i] == "(":
        right += 1
    elif S[i] == ")" and right > 0:
        right -= 1
    else:
        l_count += 1

r_count = 0
for i in range(N-1,-1,-1):
    if S[i] == ")":
        left += 1
    elif S[i] == "(" and left > 0:
        left += 1
    else:
        r_count += 1

left = "".join(["(" for _ in range(l_count)])
right = "".join([")" for _ in range(r_count)])

print(left+S+right)


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

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


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


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


left = 0  
right = 0 
l_count = 0
for i in range(N):
    if S[i] == "(":
        right += 1
    elif S[i] == ")" and right > 0:
        right -= 1
    else:
        l_count += 1

r_count = 0
for i in range(N-1,-1,-1):
    if S[i] == ")":
        left += 1
    elif S[i] == "(" and left > 0:
        left -= 1
    else:
        r_count += 1

left = "".join(["(" for _ in range(l_count)])
right = "".join([")" for _ in range(r_count)])

print(left+S+right)

Problem: p03696
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n = int(input())
s = input()
ss = s.replace('()', '')
left, right = ss.count('('), ss.count(')')
print('(' * right + s + ')' * left)

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

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


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


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
ss = s
while ss.count('()'):
    ss = ss.replace('()', '')
left, right = ss.count('('), ss.count(')')
print('(' * right + s + ')' * left)
Problem: p03696
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
from collections import deque
n = int(input())
s = input()
A = deque([])
for i in range(n):
  if s[i]=='(':
    A.append(s[i])
  elif s[i]==')':
    if len(A)!=0 and A[-1]=='(':
      A.append(s[i])
    elif len(A)==0 or A[-1]==')':
      A.appendleft("(")
      A.append(")")
cou = 0
for i in range(len(A)):
  if A[i]==')':
    cou = i
amari = len(A)-(cou+1)

for j in range(amari):
  A.append(")")
print("".join(A))

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

Expected output:
()()(())


Actual output:
()()(()


Fixed submission (verdict: Accepted):
from collections import deque
n = int(input())
s = input()
A = deque([])
l = 0
r = 0
for i in range(n):
  if s[i]=='(':
    A.append(s[i])
    l += 1
  else:
    if l!=0:
      A.append(s[i])
      l -=1
    else:
      A.appendleft("(")
      A.append(s[i])

for j in range(l):
  A.append(")")
  
print("".join(A))

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

x = int(input())
y = input()

z = y

while z.find("()") != -1:
    z = z.replace("()","")

a = z.count("(")
b = z.count(")")

ans = z

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

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

print(ans)


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

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


Actual output:
(())(())


Fixed submission (verdict: Accepted):

x = int(input())
y = input()

z = y

while z.find("()") != -1:
    z = z.replace("()","")

a = z.count("(")
b = z.count(")")

ans = y

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

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

print(ans)

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

x=0
y=0

for i in S:
    if i=="(":
        x+=1
    else:
        if x<=0:
            y+=1
        else:
            x-=1
print("("*x+S+")"*x)

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

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


Actual output:
))()()))


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

x=0
y=0

for i in S:
    if i=="(":
        x+=1
    else:
        if x<=0:
            y+=1
        else:
            x-=1
print("("*y+S+")"*x)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()

output = ''

start = 0
end = 0
for i in S:
    if i == '(':
        if end > 0:
            output += '(' * end
            output += ')' * end
            end = 0
        start += 1
        output += i
    else:
        if start > 0:
            start -= 1
            output += i
        else:
            end += 1

for i in range(start):
    output += ')'

for i in range(end):
    output += ')'

output = '(' * end + output

print(output)


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

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


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


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

output = ''

hiraki = 0
toji = 0
for i in S:
    if i == '(':
        if toji > 0:
            output = '(' * toji + output
            output += ')' * toji
            toji = 0
            hiraki += 1
            output += i
        else:
            hiraki += 1
            output += i
    else:
        if hiraki > 0:
            hiraki -= 1
            output += i
        else:
            toji += 1

for i in range(hiraki):
    output += ')'

for i in range(toji):
    output += ')'

output = '(' * toji + output

print(output)

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

for char in s:
    if char == '(': x += 1
    else: y += 1

if x < y: ans = '('*(y-x)+s
else: ans = s+')'*(x-y)
print(ans)

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

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


Actual output:
)(()())(


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
x, y, a = 0, 0, 0

for char in s:
    if char == '(': x += 1
    else: y += 1
    a = min(a, x-y)
    b = x-y

ans = '('*(-a) + s + ')'*(b-a)
print(ans)

Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()
tmp = S[-1]
index = 0
ans = ''
cnt1 = 0
cnt2 = 0
for i in range(N):
    if S[i] == ')':
        cnt1 += 1


for i in reversed(range(N)):
    if S[i] == ')':
        break
    if S[i] == '(':
        cnt2 += 1

S = '(' * (cnt1 - S.count('(') + cnt2) + S
S += ')' * cnt2

print(S)

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

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


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


Fixed submission (verdict: Accepted):
N = int(input())
S = input()
tmp = S[-1]
index = 0
ans = ''
cnt1 = 0
cnt2 = 0
for i in range(N):
    if S[i] == '(':
        cnt1 += 1
    elif cnt1 == 0:
        cnt2 += 1
    else:
        cnt1 -= 1

print('(' * cnt2 + S + ')' * cnt1)
Problem: p03696
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = input()
l = s.count("(")
r = s.count(")")
lcount = 0
rcount = 0
if n == 1:
    print("()")
elif l == 0:
    print("("* r + s)
elif r == 0:
    print(s + ")"*l)
else:
    while s[lcount] == ")":
        lcount += 1

    while s[n-1-rcount] == "(":
        rcount += 1

    res = "(" * lcount + s + ")" * rcount

    l = res.count("(")
    r = res.count(")")

    if l > r:
        res = res + ")" * (l-r)

    elif r > l:
        res = "(" * (r-l) + res

    print(res)

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

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


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


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
l = s.count("(")
r = s.count(")")
lmax = 0
if n == 1:
    print("()")
elif l == 0:
    print("("* r + s)
elif r == 0:
    print(s + ")"*l)
else:
    l = 0
    r = 0
    for c in s:
        if c == "(":
            l += 1
        else:
            r += 1
        lmax = max(r-l, lmax,0)
    
    res = "(" * lmax + s 

    l = res.count("(")
    r = res.count(")")

    if l > r:
        res = res + ")" * (l-r)

    elif r > l:
        res = "(" * (r-l) + res

    print(res)

Problem: p03696
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n = int(input())
a = input()
cr = 0
cl = 0
ans = a
for i in range(n):
  if a[i] == "(":
    cl += 1
  elif a[i] == ")":
    cr += 1
  if cl == 0 and cr != 0:
    ans = "(" + ans
    cr -= 1
  if cl != 0 and cr != 0:
    if cr >= cl:
      cr -= cl
      cl -= cl
    else:
      cr -= cr
      cl -= cr
if cl != 0:
  for i in range(cl):
    ans = ans+")"
print(ans)

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

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


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


Fixed submission (verdict: Accepted):
n = int(input())
a = input()
cr = 0
cl = 0
ans = a
for i in range(n):
  if a[i] == "(":
    cl += 1
  elif a[i] == ")":
    cr += 1
  if cl == 0 and cr != 0:
    ans = "(" + ans
    cr -= 1
  if cl != 0 and cr != 0:
    if cr >= cl:
      cl, cr = 0,cr-cl
    else:
      cl, cr = cl-cr,0
if cl != 0:
  for i in range(cl):
    ans = ans+")"
print(ans)
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
    print(S)
    if S[i] == "(":
        j = i
        while 1:
            if j >= len(S):
                break
            if S[j] == ")":
                S.pop(j)
                S.pop(i)
                i = -1
            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.4.3), verdict: Wrong Answer):
N = input()

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


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

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


Actual output:
8


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):
n = int(input())
s = input()
bf = []
af = []
st = ['*']

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

        st.append(')')
        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)