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)
๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ
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)