LogiQuest

261. Frog Jump with K

EasyDynamic Programming

A frog on stone 1 wants to reach stone n. From stone i it can jump to i + 1 … i + k at cost |h[i] − h[j]|. Print the minimum total cost.

Input format

Line 1: n k Line 2: n heights

Output format

The minimum cost.

Example 1

Input

5 3
10 30 40 50 20

Output

30

Example 2

Input

3 1
10 20 10

Output

20

Constraints

2 ≤ n ≤ 10⁵ 1 ≤ k ≤ 100

Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.

Loading the MyLoginn IDE…