#K77572. Minimum Coins for Specific Denominations

    ID: 34894 Type: Default 1000ms 256MiB

Minimum Coins for Specific Denominations

Minimum Coins for Specific Denominations

You are given an amount \(S\) and three coin denominations \(a\), \(b\) and \(c\). Your task is to determine the minimum number of coins required to obtain exactly the sum \(S\). If it is not possible to form \(S\) using any combination of the given denominations, output \(-1\).

The problem can be formulated as follows:

Given \(S, a, b, c\), find the minimum number of coins such that:

\[ \text{if } dp[i] = \min\{ dp[i-a], dp[i-b], dp[i-c] \} + 1 \quad \text{for all } i \geq 1, \]

with initial condition \(dp[0]=0\). If \(dp[S]\) remains infinity, then the answer is \(-1\).

inputFormat

The input consists of a single line containing four space-separated integers:

  • \(S\): the target sum.
  • \(a, b, c\): the three coin denominations.

You should read the input from standard input (stdin).

outputFormat

Output a single integer: the minimum number of coins needed to form \(S\). If it is impossible, output \(-1\).

The output should be written to standard output (stdout).

## sample
11 1 5 7
3