#K85242. Minimum Coin Change with Custom Denominations

    ID: 36598 Type: Default 1000ms 256MiB

Minimum Coin Change with Custom Denominations

Minimum Coin Change with Custom Denominations

In this problem, you are given an integer (amount) and a bitmask representing the available coin denominations. There are four possible coins: (1, 5, 10, 25) cents. The bitmask, when written in binary, indicates which coins are available. For example, a bitmask of (0b0111) means that the coins (1, 5,) and (10) cents are available, while (0b1001) means that the coins (1) and (25) cents are available. Your task is to determine the minimum number of coins needed to form the given (amount) using only the available coin denominations. If it is impossible to form the amount, output (-1).

inputFormat

Input is provided via standard input (stdin). The input consists of a single line containing two space-separated integers: (amount) and (d), where (d) is the integer representation of the coin denominations bitmask. For example, the input 12 7 corresponds to an (amount) of 12 and a bitmask of (0b0111).

outputFormat

Output the minimum number of coins required to form the given (amount) using the available denominations. If it is not possible to form the amount, output (-1). The result should be printed to standard output (stdout).## sample

12 7
3