import java.util.*;


public class Solution {
    /**
     * 最少货币数
     * @param arr int整型一维数组 the array
     * @param aim int整型 the target
     * @return int整型
     */
    public int minMoney (int[] arr, int aim) {
        // write code here
        if (arr == null || arr.length < 1 || aim < 0) {
            return -1;
        }
        int n = arr.length;
        int[][] dp = new int[n][aim + 1];
        for (int j = 1; j <= aim; j++) {
            if (j % arr[0] == 0) {
                dp[0][j] = j / arr[0];
            } else {
                dp[0][j] = aim + 1;
            }
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j <= aim; j++) {
                dp[i][j] = Math.min(dp[i - 1][j], aim + 1);
                if (j >= arr[i]) {
                    dp[i][j] = Math.min(dp[i][j], dp[i][j - arr[i]] + 1);
                }
            }
        }
        return dp[n - 1][aim] > aim ? -1 : dp[n - 1][aim];
    }
}