Skip to content

Commit

Permalink
Merge pull request #22 from AlgoLeadMe/5-wnsmir
Browse files Browse the repository at this point in the history
5-wnsmir
  • Loading branch information
g0rnn authored Nov 24, 2024
2 parents c52aea1 + 8a2c33b commit ba3f103
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions wnsmir/DP/9465์Šคํ‹ฐ์ปค.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys
input = sys.stdin.read

def max_sticker_score(n, stickers):
# n์ด 1์ผ ๋•Œ๋Š” ๊ฐ„๋‹จํžˆ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜
if n == 1:
return max(stickers[0][0], stickers[1][0])

# DP ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
dp = [[0] * n for _ in range(2)]

# ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
dp[0][0] = stickers[0][0]
dp[1][0] = stickers[1][0]

# n์ด 2 ์ด์ƒ์ธ ๊ฒฝ์šฐ ์ฒซ ๋‘ ์—ด์„ ์ดˆ๊ธฐํ™”
if n > 1:
dp[0][1] = dp[1][0] + stickers[0][1]
dp[1][1] = dp[0][0] + stickers[1][1]

# DP ๋ฐฐ์—ด ์ฑ„์šฐ๊ธฐ
for i in range(2, n):
dp[0][i] = max(dp[1][i-1], dp[1][i-2]) + stickers[0][i] # i-2๊นŒ์ง€ ์ฒ˜๋ฆฌ (์ ํ™”์‹)
dp[1][i] = max(dp[0][i-1], dp[0][i-2]) + stickers[1][i]

# ์ตœ๋Œ“๊ฐ’ ๋ฐ˜ํ™˜
return max(dp[0][n-1], dp[1][n-1])

# ์ž…๋ ฅ ์ฒ˜๋ฆฌ
data = input().strip().split()
T = int(data[0]) # ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
index = 1
results = []

for _ in range(T):
n = int(data[index])
stickers = [
list(map(int, data[index + 1:index + 1 + n])),
list(map(int, data[index + 1 + n:index + 1 + 2 * n]))
]
results.append(max_sticker_score(n, stickers))
index += 2 * n + 1

# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
for result in results:
print(result)

0 comments on commit ba3f103

Please sign in to comment.