Skip to content

Commit 0614040

Browse files
committed
[BOJ] #17845. 수강 과목 / 골드5 / 30분 / 성공
1 parent 56e2f98 commit 0614040

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 1. 입력받기
5+
N, K = map(int, input().split()) # N: 최대 공부 시간, K: 과목 수
6+
lectures = [[0, 0]] # 수강 과목 리스트 (중요도, 필요한 공부 시간)
7+
for _ in range(K):
8+
lectures.append(list(map(int, input().split()))) # 중요도와 공부 시간
9+
10+
# 2. DP 초기화
11+
dp = [[0] * (N + 1) for _ in range(K + 1)]
12+
13+
# 3. DP 채우기(냅색 알고리즘)
14+
for i in range(1, K + 1): # 각 과목에 대해
15+
importance = lectures[i][0] # 중요도
16+
time = lectures[i][1] # 필요한 공부 시간
17+
for j in range(1, N + 1): # 공부 시간 1부터 N까지
18+
if j >= time: # 현재 공부 시간으로 이 과목을 선택 가능 -> 더 큰 값으로 갱신
19+
dp[i][j] = max(dp[i-1][j], dp[i-1][j-time] + importance)
20+
else: # 충족할 수 없다 -> 이전 값 유지
21+
dp[i][j] = dp[i-1][j]
22+
23+
# 4. 결과 출력
24+
print(dp[K][N])

0 commit comments

Comments
 (0)