forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.py
More file actions
22 lines (19 loc) Β· 689 Bytes
/
8.py
File metadata and controls
22 lines (19 loc) Β· 689 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# μ μ N, Mμ μ
λ ₯ λ°κΈ°
n, m = map(int, input().split())
# Nκ°μ νν λ¨μ μ 보λ₯Ό μ
λ ₯ λ°κΈ°
array = []
for i in range(n):
array.append(int(input()))
# ν λ² κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
d = [10001] * (m + 1)
# λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = 0
for i in range(n):
for j in range(array[i], m + 1):
if d[j - array[i]] != 10001: # (i - k)μμ λ§λλ λ°©λ²μ΄ μ‘΄μ¬νλ κ²½μ°
d[j] = min(d[j], d[j - array[i]] + 1)
# κ³μ°λ κ²°κ³Ό μΆλ ₯
if d[m] == 10001: # μ΅μ’
μ μΌλ‘ Mμμ λ§λλ λ°©λ²μ΄ μλ κ²½μ°
print(-1)
else:
print(d[m])