14501

14501-ํ‡ด์‚ฌ

www.acmicpc.net/problem/14501

๋ธŒ๋ฃจํŠธ ํฌ์Šค

  1. s_index ๋Š” ์ˆœ์—ด๋กœ ์ธ๋ฑ์Šค๋ฅผ ๋ฝ‘์„ ๋•Œ ์‚ฌ์šฉํ•  ์ธ๋ฑ์Šค์ˆœ์„œ๋Œ€๋กœ ์ธ๋ฑ์Šค๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด

  2. ์šฐ์„  1๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๊ฒฝ์šฐ๋ถ€ํ„ฐ n ๊ฐœ ๋ชจ๋‘๋ฅผ ๋ฝ‘๋Š” ๋ฐ˜๋ณต๋ฌธ

  3. ๋ฝ‘์„ ๊ฐœ์ˆ˜๋งŒํผ ์ˆœ์—ด๋กœ ๋ฝ‘์„ ์ธ๋ฑ์Šค๋“ค์„ ์ถ”์ถœ

  4. ๋ฝ‘์€ ์ธ๋ฑ์Šค์— ๋Œ€ํ•ด์„œ ์ƒ๋‹ด์ด ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ

  5. ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ํ•ด๋‹น์˜ ๊ธˆ์•ก ์ตœ๋Œ“๊ฐ’ ์—…๋ฐ์ดํŠธ

from itertools import combinations

n = int(input())
a = [list(map(int,input().split())) for _ in range(n)]
s_index = [i for i in range(n)]

def check(indexs):
    global money, n
    # ์ˆœ์—ด๋กœ ๋ฝ‘์€ ์ธ๋ฑ์Šค๋งŒ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค
    temp = [0 for i in range(n)]
    for index in indexs:
        temp[index] = a[index]

    # temp ๊ฐ€๋Šฅํ•œ ๋ฐฐ์น˜์ธ์ง€ ๊ฒ€์‚ฌ
    for i in range(len(temp)):
        index = i
        if temp[i]:
            long = temp[i][0]
            # ์ธ๋ฑ์‹ฑ ๋ฒ”์œ„๊ฐ€ ๋„˜์–ด๊ฐ€์ง„ ์•Š๋Š”์ง€
            if index + long > n:
                return False
            # ์˜ค๋Š˜๋ถ€ํ„ฐ ์ƒ๋‹นํ•˜๋Š” ๋™์•ˆ ์•„๋ฌด๋„ ์•ˆ ์˜ค๋Š”์ง€
            for j in range(1, long):
                if temp[index + j]:
                    return False

    # ๊ฐ€๋Šฅํ•ด์„œ ์—ฌ๊ธฐ๊นŒ์ง€ ์™”์œผ๋‹ˆ ๋ˆ๊ณ„์‚ฐ์„ ํ•ด์ฃผ์ž
    money = 0
    for i in range(len(indexs)):
        money += a[indexs[i]][1]
    return True

max_answer = 0
money = 0
# 1 ๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ๊ฒƒ ๋ถ€ํ„ฐ N ๊ฐœ๋ฅผ ์ „๋ถ€ ๊ณ ๋ฅด๋Š” ์ˆœ์—ด๋“ค
for pic_num in range(1, n + 1):
    # ์ธ๋ฑ์Šค๋“ค ์ค‘์—์„œ ๋ช‡ ๊ฐœ ๊ณ ๋ฅผ์ง€ ๋งŒํผ ๋งŒํผ ์ˆœ์—ด๋กœ ๋ฝ‘์•„์ฃผ์ž
    pic_combination = list(combinations(s_index,pic_num))
    # pics ๋Š” ๋‚ด๊ฐ€ ๊ณ ๋ฅผ ์ธ๋ฑ์Šค๋“ค
    for pics in pic_combination:
        # ํ•ด๋‹น ์ธ๋ฑ์Šค๋“ค์„ ๊ณจ๋ž์„ ๋•Œ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜์ž
        if check(pics):
            # ๊ฐ€๋Šฅํ–ˆ๋‹ค๋ฉด ์ตœ๋Œ“๊ฐ’ ์—…๋ฐ์ดํŠธ
            max_answer = max(max_answer,money)

print(max_answer)

๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ

  • ์ ํ™”์‹

    dp[i] = max(p[i] + d[t[i] + i], max_p)
  • ๋์—์„œ๋ถ€ํ„ฐ ๋งค๋ฒˆ ์„ ํƒํ•ด๋‚˜๊ฐˆ ๋•Œ, ์ง€๊ธˆ๊ฒƒ์„ ์ทจํ•˜๊ณ , ์ง€๊ธˆ ๊ฒƒ ์ƒ๋‹ด์ด ๋๋‚œ ํ›„์˜ dp[i] ๋ฅผ ๋”ํ•œ ๊ฒƒ๊ณผ, ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ๋Œ“๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด์„œ ์ทจํ•ด๊ฐ

n = int(input())

# 0 ๋ฒˆ์งธ ๋‚ ์€ ์˜๋ฏธ๊ฐ€ ์—†์œผ๋‹ˆ 0์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ณ , ์ธ๋ฑ์‹ฑ์„ ์ผ์ฐจ๋ž‘ ๋งž์ถ”๊ธฐ ์œ„ํ•ด
t = [0]
p = [0]

for i in range(n):
    a, b = map(int,input().split())
    t.append(a)
    p.append(b)

d = [0] * (n + 1)
max_p = 0

# ์ธ๋ฑ์Šค n ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋บด๊ฐ€๋ฉด์„œ 1๊นŒ์ง€
for i in range(n, 0, -1):

    # ์• ์ดˆ์— ํ˜„์žฌ ๊ฒƒ์ด ํ‡ด์‚ฌ๋‚  ์ „๊นŒ์ง€ ๊ฐ€๋Šฅํ•œ์ง€ ๊ฒ€์ฆ
    if t[i] + i <= n + 1:
        #
        if i + t[i] > n:
            f = 0
        else:
            f = d[i + t[i]]
        d[i] = max(p[i] + f, max_p)
        max_p = d[i]
    else:
        d[i] = max_p

print(max_p)

Last updated