Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n, x;
- int niza[105];
- int dp[1000003];
- int coins(int x_left) {
- if(x_left == 0) {
- return 0;
- }
- if(dp[x_left] != -1) {
- return dp[x_left];
- }
- int result = 2e9;
- for(int i = 0; i < n; i++) {
- if(x_left - niza[i] >= 0) {
- result = min(result, coins(x_left - niza[i]) + 1);
- }
- }
- return dp[x_left] = result;
- }
- int main()
- {
- cin >> n >> x;
- for(int i = 0; i <= x; i++) {
- dp[i] = -1;
- }
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- }
- if(coins(x) == 2e9) {
- cout << -1 << endl;
- }
- else cout << coins(x) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement