[BOJ] 2294 동전 2

Time Lapse :NONE

2294.cpp

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
41
42
43
44
45
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>

using namespace std;

#define MAXSIZE 1000000
int N, K;
int num;
int cache[MAXSIZE];
int ans = -1;

int dp(int money)
{
int &ret = cache[money];
if(ret!=11000)
return ret;
ret = 10001;
for(int i=1;i<=money/2;i++)
{
ret = min(ret,dp(i)+dp(money-i));
}
return ret;
}
int main(void)
{
cin>>N>>K;
for(int i=0;i<MAXSIZE;i++)
cache[i]=11000;

for(int i=0;i<N;i++)
{
cin>>num;
cache[num]=1;
}
dp(K);
if(cache[K]>10000)
cout<<"-1"<<endl;
else
cout<<cache[K]<<endl;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2294/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.