Algorithm/SWEA
[SWEA - D3] 5215. ํ๋ฒ๊ฑฐ ๋ค์ด์ดํธ
hello_u
2023. 5. 10. 22:52
๋์ ์ฝ๋
def DFS(L,score,kcal):
global res
if kcal > k:
return
if score > res:
res = score
if L == n:
return
s,c = arr[L]
DFS(L+1,score+s, kcal+c)
DFS(L+1,score,kcal)
T = int(input())
for t in range(1,T+1):
n,k = map(int,input().split())
arr = []
res = 0
for _ in range(n):
a,b = map(int,input().split())
arr.append((a,b))
DFS(0,0,0)
print("#"+str(t) , res )
DFS ๋ฅผ ์ด์ฉํ ํ์ด
ํ๋ฒ๊ฑฐ ์ฌ๋ฃ์(n), ๋์ง ๋ง์์ผ ํ๋ ์นผ๋ก๋ฆฌ์(k)์ ์ ๋ ฅ๋ฐ๊ณ
๋ฆฌ์คํธ๋ฅผ ์์ฑํ๊ณ ๋ง๊ณผ ์นผ๋ก๋ฆฌ๋ฅผ ์ ์ฅํ ํ
์นผ๋ก๋ฆฌ๊ฐ ๋์ด๊ฐ๋ฉด ํ์์ ์ํX
๊ธฐ์กด์ ์ ์ฅ๋ ์ต๋ ๋ง๋ณด๋ค ํฌ๋ฉด ๊ฐฑ์
๋ฆฌ์คํธ๋ฅผ ๋ค๋๋ฉด return
์ฌ๋ฃ๋ฅผ ์ผ์ ๋, ์ ์ผ์ ๋ ๋ฅผ ๊ฐ๊ฐ ํ์