public List<Integer> cheapestJump(int[] A, int B) {
List<Integer> res = new ArrayList<>();
if (A == null || A.length == 0 || A[A.length - 1] == -1) {
// dp[i] is the min cost to reach position i from the start
Arrays.fill(dp, Integer.MAX_VALUE);
// preIndex[i] store the previous index of i in the min cost path
int[] preIndex = new int[n];
Arrays.fill(preIndex, -1);
for (int i = n - 2; i >= 0; i--) {
// Ignore position we cannot reach
if (A[i] == -1) continue;
for (int j = i + 1; j <= Math.min(i + B, n - 1); j++) {
if (dp[j] == Integer.MAX_VALUE) continue;
if (A[i] + dp[j] < dp[i]) {
// Cannot reach A[n - 1] from A[0]
if (dp[0] == Integer.MAX_VALUE) {
for (int index = 0; index != -1; index = preIndex[index]) {