# 2504 괄호의 값

문제 : <https://www.acmicpc.net/problem/2504>

```python
import sys
input = sys.stdin.readline
brackets = list(input().strip())
stack=[]

# 유효한 괄호열인지 확인
for brkt in brackets:
    if stack:
        if stack[-1] == '(' and brkt == ')':
            stack.pop()
            continue
        elif stack[-1] == '[' and brkt == ']':
            stack.pop()
            continue
    stack.append(brkt)

if stack:
    print(0)
else:
    for brkt in brackets:
        if brkt == ')':
            if stack[-1] == '(':
                stack.pop()
                stack.append(2)
            else:
                piece = 0
                while(stack[-1] != '('):
                    piece += stack.pop()
                stack.pop()
                stack.append(2 * piece)
        elif brkt == ']':
            if stack[-1] == '[':
                stack.pop()
                stack.append(3)
            else:
                piece = 0
                while(stack[-1] != '['):
                    piece += stack.pop()
                stack.pop()
                stack.append(3 * piece)
        else:
            stack.append(brkt)

    print(sum(stack))
```

유효한 괄호열인거 확인하면

스택에 하나씩 넣으면서 ()나 \[]이 만들어지면 숫자 만들기.

숫자 안 만들어지는 오른쪽 괄호 나오면 왼쪽 괄호 나올 때까지

숫자 다 더한 후 괄호 종류 따라 x2 or x3

다 끝나면 숫자만 남으니까 sum


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lazyartisan.gitbook.io/note/main-page/algorithm/undefined-1/2504.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
