Skip to content

Instantly share code, notes, and snippets.

@velasquezkevin
Created October 20, 2019 06:13
Show Gist options
  • Select an option

  • Save velasquezkevin/eb0e1b41aefa7985096959665faf22a0 to your computer and use it in GitHub Desktop.

Select an option

Save velasquezkevin/eb0e1b41aefa7985096959665faf22a0 to your computer and use it in GitHub Desktop.
Problem B. Bracket Sequence - ICPC 19/10/2019
n = int(input())
x = input().split(" ")
i = total = 0
def operar(op = True, subtotal = 1):
global i, x
while True:
el = x[i]
i += 1
if el == ")":
return subtotal
elif el == "(":
if op: subtotal *= operar(not op, 1 if not op else 0)
else: subtotal += operar(not op, 1 if not op else 0)
else:
val = int(el)
if op: subtotal *= val
else: subtotal += val
def main():
global i, n, total, x
while i < n:
if x[i] == "(":
i += 1
total += operar()
elif x[i] == ")":
print("\nclosure\n")
else:
total += int(x[i])
i += 1
print(total)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment