PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000002. Ếch nhảy 2 (Frog 2)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ếch nhảy 2 (Frog 2)

Frog 2

Source: AtCoder

Problem Statement

There are NN stones in a row, numbered from 11 to NN, with heights hih_i.

A frog starts on stone 11. From stone ii, it may jump to any stone jj such that 1≤j−i≤K1\le j-i\le K and j≤Nj\le N. Such a jump costs ∣hi−hj∣|h_i-h_j|.

Find the minimum total cost required to reach stone NN.

Input

The first line contains NN and KK: the number of stones and the maximum number of positions advanced by a single jump.

The second line contains NN integers h1,h2,…,hNh_1,h_2,\ldots,h_N, the stone heights.

Output

Print one integer: the minimum total cost of all jumps.

Subtasks

  • Subtask 1 (20 points): 2≤N≤202\le N\le 20; 1≤K≤1001\le K\le 100; 1≤hi≤1041\le h_i\le 10^4 (1≤i≤N1\le i\le N).
  • Subtask 2 (30 points): 2≤N≤5002\le N\le 500; 1≤K≤1001\le K\le 100; 1≤hi≤1041\le h_i\le 10^4 (1≤i≤N1\le i\le N).
  • Subtask 3 (50 points): 2≤N≤1052\le N\le 10^5; 1≤K≤1001\le K\le 100; 1≤hi≤1041\le h_i\le 10^4 (1≤i≤N1\le i\le N).

Examples

Input

5 3
10 30 40 50 20

Output

30

Explanation

Choose jumps 1→2→51\to2\to5; the second jump spans 33 indices, allowed by K=3K=3. Total cost: ∣10−30∣+∣30−20∣=30|10-30|+|30-20|=30.