-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimizing_coins.cpp
More file actions
41 lines (37 loc) · 860 Bytes
/
Copy pathminimizing_coins.cpp
File metadata and controls
41 lines (37 loc) · 860 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
41
#include <bits/stdc++.h>
using namespace std;
#define m (int)(1e9+7)
typedef long long ll;
#define input(v) for (auto &i : v) cin >> i;
#define print(v) for (auto &j : v) cout << j << " "; cout << "\n";
#define inf (ll)(1e18)
const int siz=(int)(1e6+1);
ll f[siz];
bool visited[siz];
ll coin[101];
ll func(int n,int x)
{
if(x<0) return inf;
if(x==0) return 0;
if(visited[x]) return f[x];
visited[x]=true;
for(int i=1;i<=n;i++) f[x]=min(f[x],1+func(n,x-coin[i]));
return f[x];
}
void solve()
{
int n,x;
cin>>n>>x;
for(int i=0;i<siz;i++) f[i]=inf;
for(int i=1;i<=n;i++) cin>>coin[i];
cout<<(func(n,x) < inf ? func(n,x) : -1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int testcases=1;
//cin>>testcases;
while(testcases--) solve();
return 0;
}