forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.cpp
More file actions
40 lines (33 loc) Β· 979 Bytes
/
8.cpp
File metadata and controls
40 lines (33 loc) Β· 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> arr;
int main(void) {
// μ μ N, Mμ μ
λ ₯λ°κΈ°
cin >> n >> m;
// Nκ°μ νν λ¨μ μ 보λ₯Ό μ
λ ₯ λ°κΈ°
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr.push_back(x);
}
// ν λ² κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
vector<int> d(m + 1, 10001);
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = arr[i]; j <= m; j++) {
// (i - k)μμ λ§λλ λ°©λ²μ΄ μ‘΄μ¬νλ κ²½μ°
if (d[j - arr[i]] != 10001) {
d[j] = min(d[j], d[j - arr[i]] + 1);
}
}
}
// κ³μ°λ κ²°κ³Ό μΆλ ₯
if (d[m] == 10001) { // μ΅μ’
μ μΌλ‘ Mμμ λ§λλ λ°©λ²μ΄ μλ κ²½μ°
cout << -1 << '\n';
}
else {
cout << d[m] << '\n';
}
}