728x90
https://www.acmicpc.net/problem/2294
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> coin(n);
for (int i = 0; i < n; i++) {
cin >> coin[i];
}
sort(coin.begin(), coin.end());
vector<vector<int>> dp(n, vector<int>(k + 1, 100001));
for (int i = 1; i <= k; i++) {
if (i % coin[0] == 0) {
dp[0][i] = i / coin[0];
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= k; j++) {
if (j < coin[i]) {
dp[i][j] = dp[i - 1][j];
}
else if (j == coin[i]) {
dp[i][j] = 1;
}
else {
dp[i][j] = min(dp[i - 1][j], dp[i][j - coin[i]] + 1);
}
}
}
/*cout << " ";
for (int i = 0; i <= k; i++) {
cout << setw(3) << i;
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << setw(3) << coin[i];
for (int j = 0; j <= k; j++) {
if(dp[i][j] != 100001)
cout << setw(3) << dp[i][j];
else
cout << setw(3) << 0;
}
cout << endl;
}*/
if (dp[n - 1][k] != 100001) cout << dp[n - 1][k];
else cout << -1;
return 0;
}
728x90
'알고리즘 문제' 카테고리의 다른 글
[백준] 5338번 마이크로소프트 로고 (0) | 2020.03.13 |
---|---|
[백준] 1699번 제곱수의 합 (0) | 2020.03.13 |
[백준] 1520번 내리막 길 (0) | 2020.03.11 |
[백준] 2293번 동전 1 (0) | 2020.03.08 |
[백준] 1057번 토너먼트 (0) | 2020.03.07 |