728x90
๋ฌธ์
ํ์ด
๊ฒฝ๋ก๋ ์ ์ผํ๋ฏ๋ก ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ๋ ๋ฐ๋ก ๊ตฌํํ์ง ์์๋ค.
ํธ๋ฆฌ์ ๋ฆฌํ ๋ ธ๋๊น์ง ์ฌ๊ท๋ฅผ ํตํด ์ด๋ํ๊ณ , ๋ฆฌํด ๊ฐ์ ํตํด ๋ฌธ์ ๋ฅผ ํ์๋ค.
์ฝ๋
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
n = int(input())
tree = [[] for _ in range(n + 1)]
for i in range(2, n + 1):
type, amount, root = input().split()
amount, root = int(amount), int(root)
if type == 'W':
amount = -amount
tree[root].append((i, amount))
def dfs(x, amount):
for next, nextAmount in tree[x]:
tmp = dfs(next, nextAmount)
if tmp > 0:
amount += tmp
return amount
print(dfs(1, 0))
๋ง๋ฌด๋ฆฌ
ํ๋ฒ์ ํ๋ ค์ ์กฐ๊ธ ๋นํฉํ๋ค..ใ ใ
728x90