Minimum Asphalt to repair all damaged locations of the road

Can anyone help me to understand the logic?

There is a highway that has damages at N (4<=N<=100) locations. Each damage location is represented by a number a (0<=a<=10000). You need to repair these damages with Asphalt. The damage is repaired with these rules:

  1. Asphalt can be spread on the road with a minimum stretch of K (1<=K<=10000) i.e. each time you put asphalt, it will be spread to k consecutive locations. eg. if the damage is at locations 2, 5 and K=3 then the first asphalt spread is from 0-2 or 1-3 or 2-4 and the second is from 3-5 or 4-6 or 5-7.
  2. You need to spread minimum Asphalt to repair all damaged locations of the road. For the above example, the minimum asphalt required is 4 units ( 2-4 and 3-5) because 3-4 is overlapping.

Example test cases:

Input:-

N K

A[0] A[1] ……… A[N-1]

Output:-

Minimum Asphalt area to repair all damages.

#first case

10 2

0 10 2 12 4 14 6 16 8 18

output: 15

#second case

4 3

3 9 11 8

output: 7

#third case

8 3

2 7 20 5 19 9 6 22

output: 12

Can someone help me with this problem?

Testcase 1:
10 2
0 10 2 12 4 14 6 16 8 18

#step 1 take the input and sort it
[0 2 4 6 8 10 12 14 16 18]

#step 2, find out over which intervals you have to spread asphalt. Since k = 2 you can spread it like this:
from 0 to 1, from 1 to 2, from 4 to 5, from 5 to 6, from 8 to 9, from 9 to 10, from 12 to 13, from 13 to 14, from 16 to 17, from 17 to 18.
(Notice how each value from your sorted array is inside one of the intervals. Meaning you have fixed all road damages)

The combined intervals would look like this: [0, 2]+[4, 6]+[8, 10]+[12, 14]+[16, 18]
The combined intervals would have these sizes: 3+3+3+3+3
in total, you would spread asphalt of length: sum_of_interval_sizes = 3+3+3+3+3 = 15

This is already half the algorithm you need. If you need more help feel free to ask.
Also, link the problem next time.