Question
The Minimum Coin Vault

You are given an array coins of coin denominations and a total amount. You have an unlimited supply of each denomination. Return the minimum number of coins needed to make up the amount exactly. If it is impossible, return -1.

Complete the function coinChange(coins, amount).

Input

The first line contains an integer KaTeX can only parse string typed expression — the number of coin denominations.
The second line contains KaTeX can only parse string typed expression space-separated integers.
The third line contains the integer KaTeX can only parse string typed expression.

Output

A single integer — the minimum number of coins needed, or KaTeX can only parse string typed expression if impossible.

Example
Example 1:
Input
3

1 2 5
11
Output
3
Explanation
11 = 5+5+1, using 3 coins.

Example 2:
Input
1

2
3
Output
-1
Explanation
Amount 3 cannot be made with only 2-coins.

Example 3:
Input
1

1
0
Output
0
Explanation
Zero coins needed to make amount 0.

Online