Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 14501๋ฒˆ - ํ‡ด์‚ฌ (DP๋กœ ํ’€๊ธฐ)

๊ฐ์ž ๐Ÿฅ” 2022. 5. 17. 00:46
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ๋งํฌ

https://www.acmicpc.net/problem/14501

 

14501๋ฒˆ: ํ‡ด์‚ฌ

์ฒซ์งธ ์ค„์— ๋ฐฑ์ค€์ด๊ฐ€ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ด์ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

๋‚ด๊ฐ€ ํ‘ผ ์ฝ”๋“œ

var n = Int(readLine()!)!
var tp: [[Int]] = []
var dp = Array(repeating: 0, count: 100)

for i in 0..<n {
    tp.append(readLine()!.split(separator: " ").map { Int(String($0))! })
}

for i in 0..<n {
    if dp[i] > dp[i+1] {
        dp[i+1] = dp[i]
    }

    if dp[i+tp[i][0]] < dp[i] + tp[i][1] {
        dp[i+tp[i][0]] = dp[i] + tp[i][1]
    }
}

print(dp[n])
๋ฐ˜์‘ํ˜•