문제
https://www.acmicpc.net/problem/2294
해설
100 * 10000을 해도 100만밖에 되지 않기 때문에 그냥 풀기
코드
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
|
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int INF = 87654321;
int n,k,cost[104],ret[10004],tmp;
int main(){
cin >> n >> k;
fill(ret,ret+10004,INF);
int cnt=0;
for(int i=0;i<n;i++){
cin >> tmp;
if(tmp<=10000){
cost[cnt] = tmp;
cnt++;
ret[tmp] = 1;
}
}
for(int i=1;i<=k;i++){
for(int j=0;j<cnt;j++){
if(i-cost[j]>0) ret[i] = min(ret[i],ret[i-cost[j]]+1);
}
}
if(ret[k] >= INF){
cout << -1;
return 0;
}
cout << ret[k];
return 0;
}
|
cs |
'백준 알고리즘(C++)' 카테고리의 다른 글
백준 12852번 1로 만들기 2 ( C++ ) (0) | 2024.09.18 |
---|---|
백준 4811번 알약 ( C++ ) (0) | 2024.09.17 |
백준 2240번 자두나무 ( C++ ) (0) | 2024.09.17 |
백준 1103번 게임 ( C++ ) (0) | 2024.09.15 |
백준 17070번 파이프 옮기기 1 ( C++ ) (0) | 2024.09.15 |