Grandpa Quentin's Game
Ryan and Jake are big fans of movies and board games. Since the cinemas are closed, they decide to have some fun with a game they found at Grandpa Quentin's. The game has the following rules:
The game board consists of N cells arranged in a row, numbered from 1 to N.
Each cell contains an integer.
The player starts at cell number 1 and must reach cell number N, moving only forward.
Players cannot move outside the boundaries of the board.
The maximum step a player can take is K. This means from cell number x, a player can move to any cell between x + 1 and x + K, inclusive.
The player's score is the sum of the numbers in all the cells they step on during the game.
Ryan wants to maximize his score, while Jake aims to minimize his. They are curious about the difference in their scores when they use completely opposite strategies. Can you help them find this difference?
Input
The first line contains two natural numbers, N and K.The second line contains N integers a[1]
, a[2]
, ..., a[n]
, representing the numbers in each cell from the first to the last. 1 ≤ N, K ≤ 10^6
, −10^9
≤ a[i]
≤ 10^9
Output
Output a single integer: the difference between Ryan's and Jake's scores.