ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

15486๋ฒˆ: ํ‡ด์‚ฌ 2

์ฒซ์งธ ์ค„์— N (1 ≤ N ≤ 1,500,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— Ti์™€ Pi๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์„œ ์ฃผ์–ด์ง€๋ฉฐ, 1์ผ๋ถ€ํ„ฐ N์ผ๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ti ≤ 50, 1 ≤ Pi ≤ 1,000)

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๊ธฐ์กด์— ํ’€์—ˆ๋˜ ํ‡ด์‚ฌ ๋ฌธ์ œ์—์„œ N์˜ ๋ฒ”์œ„๊ฐ€ ์ปค์ง„ ๋ฌธ์ œ์ด๋‹ค. N์˜ ๋ฒ”์œ„๊ฐ€ ์ปค์ง์— ๋”ฐ๋ผ DFS๋กœ ํ’€์ง€ ์•Š๊ณ  DP๋กœ ํ’€์–ด์•ผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ์ตœ๋Œ€ ์ˆ˜์ต์„ ์™„์ „ ํƒ์ƒ‰์„ ํ†ตํ•ด ์ฐพ์ง€ ์•Š๊ณ , ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ํ†ตํ•ด ๊ฐ’์„ ๋ˆ„์ ์‹œ์ผœ๊ฐ€๋ฉฐ N์ผ ๊นŒ์ง€ ์ง„ํ–‰์‹œ์ผœ์•ผ ํ•œ๋‹ค.

 

 ์ ํ™”์‹์€ ํ˜„์žฌ ๋‚ ์งœ + ์ผํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋‚ ์งœ <= N์ธ ๊ฒฝ์šฐ์—, ํ•ด๋‹น ์ผ์ž์˜ ์ˆ˜์ต์ด ๊ธฐ์กด์˜ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ์ผ์„ ํ•˜๋Š” ๊ฒƒ์ด ์ด๋“์ธ์ง€๋ฅผ ๊ณ„์‚ฐํ•ด ์ฃผ๋ฉด ๋œ๋‹ค

 

์ฝ”๋“œ

from sys import stdin


if __name__ == "__main__":
    n = int(stdin.readline())
    dp = [0] * (n + 1)
    schedule = [list(map(int, stdin.readline().split())) for _ in range(n)]

    for cur_day in range(n):
        spend_day, pay = schedule[cur_day]
        if cur_day + spend_day <= n:
            dp[cur_day + spend_day] = \
                max(dp[cur_day + spend_day], dp[cur_day] + pay)

        dp[cur_day + 1] = max(dp[cur_day + 1], dp[cur_day])

    print(dp[n])

 `dp[n]`์— ์ตœ๋Œ€ ์ˆ˜์ต์„ ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด, `dp[cur_day + 1] = max(dp[cur_day + 1], dp[cur_day])`๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€