Last Updated: 1 Oct, 2025

Dynamic Divisor Path

Moderate

Problem statement

You are given an array A of N distinct positive integers. You must find the minimum cost to travel from index 0 to index N-1.


From your current index i, you can make two types of forward moves:

Step: Move to the next index, i+1. This move has a cost of 1.


Jump: Move to a future index j (where j > i). This move has a cost of j - i. A jump is only possible if the value at the destination, A[j], is a multiple of the value at your current location, A[i].


There is a special rule: whenever you successfully complete a jump from i to j, the value A[j] is permanently incremented by 1. This change can affect which future jumps are possible.


Your task is to find the minimum possible total cost to reach index N-1.


Input Format:
The first line contains an integer N, the size of the array.

The second line contains N space-separated integers, representing the elements of array A.


Output Format:
Print a single integer representing the minimum possible cost to travel from index 0 to N-1.


Note:
This problem can be modeled as a shortest path problem on a graph where the nodes are the indices 0 to N-1.

Since edge weights (costs) are not uniform (they can be 1 or j-i), an algorithm like Dijkstra's is required to find the minimum cost.