๋ฐฑ์ค 1932๋ฒ
import sys
input = sys.stdin.readline
n = int(input())
n_tree = []
for _ in range(n):
leaf = list(map(int, input().split()))
n_tree.append(leaf)
# ๊น์ด๊ฐ ์ฆ๊ฐํ ๋ ๋ง๋ค ๊น์ด ๊ฐ๋งํผ์ ๋ฐฐ์ดํฌ๊ธฐ๊ฐ ์ง์ ๋๋ค.
dp = [[0 for _ in range(i)] for i in range(1, n+1)]
dp[0][0] = n_tree[0][0] # ์ฒซ ๋ฒ์งธ ์์ ๋ฃ์ด์ ์์
for i in range(1, n):
for j in range(i+1):
if j == 0:
dp[i][j] = dp[i-1][0] + n_tree[i][0]
elif j == i:
dp[i][j] = dp[i-1][j-1] + n_tree[i][j]
#dpeth์ ์ฒซ ๊ฐ๊ณผ ๋ ๊ฐ์ ์ด์ depth์ ๋์ผ ์ธ๋ฑ์ค ๊ฐ์ ๋ํ ๊ฐ์ด ๋ค์ด๊ฐ๋ค.(๋น๊ตํ ํ์ ์์)
else:
# ๋น๊ต ๋์์ด ํ์ํ ๊ฒฝ์ฐ
# ์ด์ depth์ 2๊ฐ์ ๊ฐ๊ณผ ๋น๊ตํ์ฌ ํฐ ๊ฐ ๋ฃ๊ธฐ
com1 = dp[i-1][j-1] + n_tree[i][j]
com2 = dp[i-1][j] + n_tree[i][j]
if com1 > com2:
dp[i][j] = com1
else:
dp[i][j] = com2
print(max(dp[n-1]))
์ฒ์์ ํ์๋ ๋ฐฉ๋ฒ์ด ์๋ชป๋์๋ค๋ ๊ฒ์ ๊นจ๋ซ๊ณ ..
์ด๋ป๊ฒ ํ์ด์ผ๋์ง ํ์ด๋ฅผ ์ฐพ์๋ณด๊ณ ๋ค์ ํ์ด๋ณด์์ต๋๋ค.
์์ ๋ฌธ์ ์์ ์ค์ํ ๋ถ๋ถ์ dp[0][0] = n_tree[0][0]
๋ฅผ ๋ฃ์์ ๊ฒฝ์ฐ, for i in range(1,n)
๋ก ์์ํด์ผ ํ๋ค๋ ๊ฒ์
๋๋ค.
์๋ฃ์์ ๊ฒฝ์ฐ์๋for i in range(n)
์์ํด์ผ ํฉ๋๋ค.
ํ์ง๋ง ์ ์๊ฐ์๋ dp[0][0]
์ ๊ฐ์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค ์๊ณ ์์ผ๋.. ๊ฐ๋
์ฑ์ ์ํด ๋ฃ์ด์ฃผ๊ฒ ์ข์ ๊ฒ๊ฐ๋ค๋ ์๊ฐ์ด๋ญ๋๋ค. ๋ฌผ๋ก ์ ์ ์๊ฐ์ ๋ฐ๋ณต ํ์๋ ์ค์ด๋๋ ๊ธฐ๋ฅ ์์๋ ์ข๊ณ ..
ํ๋ฆฐ ์ฝ๋ ๊ณต๊ฐ๐
import sys
input = sys.stdin.readline
n = int(input())
n_tree = []
for _ in range(n):
leaf = list(map(int, input().split()))
n_tree.append(leaf)
dp = [[0 for _ in range(2**i)] for i in range(1, n+1)]
# ๋ฉ๋ชจ๋ฆฌ์ด๊ณผ ์ฌ๊ธฐ์ ๋ฐ์ํ๋ ๋ฏ..
dp[0][0] = n_tree[0][0]
for i in range(1, n):
index = 0
for j in range(len(n_tree[i])-1):
dp[i][index] = n_tree[i][j] + dp[i-1][j]
dp[i][index+1] = n_tree[i][j+1] + dp[i-1][j]
index += 2
print(max(dp[n-1]))
depth๊ฐ ์ฆ๊ฐํ ๋ ๋ง๋ค 2 ^ depth๋งํผ์ ๊ณ์ฐ์ด ๋ฐ์ํ๋ ๊ฒ์ ๊ตฌํํ๋ ค ํ์ง๋ง...ใ ใ ๋งํจ ๐