Skip to content

Commit

Permalink
Create 평범한 배낭.py
Browse files Browse the repository at this point in the history
2024-03-24 + 평범한 배낭
  • Loading branch information
pu2rile committed Mar 25, 2024
1 parent 1ab9efd commit 742d5ab
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pu2rile/동적 프로그래밍/평범한 배낭.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
N, K = map(int, input().split()) #입력으로 주어지는 수는 모두 정수
item = [[0,0]]
bag = [[0 for _ in range(K + 1)] for _ in range(N + 1)]
#가로 - 가방 1~K까지의 무게, 세로 - 물건 N개의 개수로 이루어진 2차원 배열

for _ in range(N): #물건의 최대 개수(N)만큼 입력
item.append(list(map(int, input().split())))


#알고리즘
for i in range(1, N + 1): #물건
for j in range(1, K + 1): #최대 무게
w = item[i][0]
v = item[i][1]

if j < w:
bag[i][j] = bag[i - 1][j] #w(eight)보다 작으면 위의 값을 그대로 가져옴
else:
bag[i][j] = max(v + bag[i - 1][j - w], bag[i - 1][j])
#bag[i][j] = max(현재 물건 가치 + bag[이전 물건][현재 가방 무게-현재 물건 무게], bag[이전 물건][현재 가방 무게])

print(bag[N][K])

0 comments on commit 742d5ab

Please sign in to comment.