척척석사가 되어보자
백준 2294 문제 본문
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
|
#include <iostream>
#include <vector>
using namespace std;
int main(){
int d[10001];
vector<int> a;
int n,k;
cin >> n >> k;
int nn = n;
while(n--){
int m;
cin >> m;
a.push_back(m) ;
}
d[0] = 0;
for(int i=1; i<=k; i++){
d[i] = 10001;
}
for(int i=0; i<nn; i++){
for(int j=a[i]; j<=k; j++){
d[j] = (d[j]<d[j-a[i]]+1)?(d[j]):(d[j-a[i]]+1);
}
}
if(d[k]==10001){
cout << "-1" <<'\n';
}
else
cout << d[k] << '\n';
return 0;
}
|
cs |
d[0] 부터 차근차근 채워보기