File tree Expand file tree Collapse file tree 1 file changed +14
-12
lines changed Expand file tree Collapse file tree 1 file changed +14
-12
lines changed Original file line number Diff line number Diff line change 6
6
7
7
8
8
C , N = map (int , sys .stdin .readline ().split ())
9
- unit_price = [None ] * N
10
- unit_amount = [None ] * N
9
+ group_cost = [None ] * N
10
+ group_size = [None ] * N
11
11
for i in range (N ):
12
- unit_price [i ], unit_amount [i ] = map (int , sys .stdin .readline ().split ())
12
+ group_cost [i ], group_size [i ] = map (int , sys .stdin .readline ().split ())
13
13
14
14
15
15
@cache
@@ -19,18 +19,20 @@ def find_min_price(i = 0, c = C) -> int:
19
19
# C명을 유치했으므로, 이제 더 비용을 안 소모해도 됨.
20
20
return 0
21
21
22
- min_price = sys .maxsize
23
- max_n_units = math .ceil (c / unit_amount [i ]) # 적어도 c명을 유치하기 위해 투자해야 하는 최소 횟수
22
+ min_cost = sys .maxsize
23
+ max_possible_groups = math .ceil (c / group_size [i ]) # 적어도 c명을 유치하기 위해 투자해야 하는 최소 횟수
24
24
25
25
if i == N - 1 :
26
26
# i번째 도시가 마지막 도시이면 선택권이 없이 투자에 올인 해야한다.
27
- return max_n_units * unit_price [i ]
28
-
29
- for n_units in range (max_n_units + 1 ):
30
- price = find_min_price (i + 1 , c - n_units * unit_amount [i ]) + n_units * unit_price [i ]
31
- if min_price > price :
32
- min_price = price
33
- return min_price
27
+ return max_possible_groups * group_cost [i ]
28
+
29
+ # 0 부터 max_possible_groups 번까지 투자해보면서 최소 비용을 찾는다.
30
+ for n_groups in range (max_possible_groups + 1 ):
31
+ # i번째 도시에 n_groups 번 투자하는 경우:
32
+ cost = find_min_price (i + 1 , c - n_groups * group_size [i ]) + n_groups * group_cost [i ]
33
+ if min_cost > cost :
34
+ min_cost = cost
35
+ return min_cost
34
36
35
37
36
38
print (find_min_price ())
You can’t perform that action at this time.
0 commit comments