Submission #2902340


Source Code Expand

Copy
lines = $stdin.read
array = lines.split("\n")
N = array[0].to_i
S = array[1]

cnt,pre = 0,0
for i in 0...N
  if S[i] == '('
    cnt+=1
  else
    cnt-=1
    pre = [pre,-cnt].max if cnt<0
  end
end

ans = (['(']*pre).join+S
n = N+pre

cnt = 0
for i in 0...n
  if ans[i] == '('
    cnt+=1
  else
    cnt-=1
  end
end

ans = ans+([')']*cnt).join

puts ans

Submission Info

Submission Time
Task D - Insertion
User hiroyuking
Language Ruby (2.3.3)
Score 400
Code Size 381 Byte
Status
Exec Time 7 ms
Memory 1788 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 s1.txt, s2.txt, s3.txt
All 400 / 400 in1.txt, in2.txt, in3.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
in1.txt 7 ms 1788 KB
in2.txt 7 ms 1788 KB
in3.txt 7 ms 1788 KB
in4.txt 7 ms 1788 KB
in5.txt 7 ms 1788 KB
in6.txt 7 ms 1788 KB
in7.txt 7 ms 1788 KB
in8.txt 7 ms 1788 KB
in9.txt 7 ms 1788 KB
s1.txt 7 ms 1788 KB
s2.txt 7 ms 1788 KB
s3.txt 7 ms 1788 KB