-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2504.py
More file actions
57 lines (55 loc) · 1.33 KB
/
2504.py
File metadata and controls
57 lines (55 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# () = 2
# [] = 3
# (x) = 2 x x
# [x] = 3 x x
# xy = x + y
def solve():
answer = 0
stack = []
for c in input_list:
if c not in ('(', ')', '[', ']'):
return False
if c in ('(', '['):
stack.append(c)
elif c == ')':
if not stack:
return False
last = stack.pop()
val = 0
while stack and str(last).isdigit():
val += last
last = stack.pop()
if last != '(':
return False
if val == 0:
stack.append(2)
else:
stack.append(val * 2)
elif c == ']':
if not stack:
return False
last = stack.pop()
val = 0
while stack and str(last).isdigit():
val += last
last = stack.pop()
if last != '[':
return False
if val == 0:
stack.append(3)
else:
stack.append(val * 3)
else:
return False
for elem in stack:
if not str(elem).isdigit():
return False
else:
answer += elem
return answer
input_list = list(input().rstrip())
result = solve()
if not result:
print(0)
else:
print(result)