Skip to content

Commit 1527483

Browse files
authored
Create 12865_평범한 배낭(G5).py
1 parent f84a44b commit 1527483

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 안녕 문제와 똑같은 방식
2+
# 버틸 무게 (K)를 넘지 않는 선에서(cost[j-w[i]]) 한 배낭에 넣을 수 있는 물건들의 가치합 저장(cost)
3+
# 최댓값(max(cost)) 출력
4+
5+
# ex) 입력
6+
# 4 7
7+
# 6 13
8+
# 4 8
9+
# 3 6
10+
# 5 12
11+
12+
# cost = [0, 0, 0, 6, 8, 12, 13, 14]
13+
14+
15+
# 2
16+
import sys
17+
input=sys.stdin.readline
18+
N, K=map(int, input().split()) # N : 물품 수, K : 버틸 무게
19+
20+
# l : [W,V]로 구성
21+
l=[]
22+
cost=[0]*(K+1)
23+
for i in range(N):
24+
W, V = map(int, input().split()) # W : 물건 무게, V : 물건의 가치
25+
l.append([W,V])
26+
27+
for i in range(N):
28+
for j in range(K, l[i][0]-1,-1):
29+
cost[j]=max(cost[j], cost[j-l[i][0]]+l[i][1])
30+
print(max(cost))
31+
32+
33+
34+
# 1 : 첫 풀이 (리스트 2개)
35+
N, K=map(int, input().split()) # N : 물품 수, K : 버틸 무게
36+
37+
w=[]
38+
v=[]
39+
40+
cost=[0]*(K+1)
41+
for i in range(N):
42+
W, V = map(int, input().split()) # W : 물건 무게, V : 물건의 가치
43+
w.append(W)
44+
v.append(V)
45+
46+
for i in range(N):
47+
for j in range(K, w[i]-1,-1):
48+
#
49+
cost[j]=max(cost[j], cost[j-w[i]]+v[i])
50+
print(max(cost))

0 commit comments

Comments
 (0)