#K15816. Obstacle Course Stress Verification

    ID: 24441 Type: Default 1000ms 256MiB

Obstacle Course Stress Verification

Obstacle Course Stress Verification

In this problem, you are given an obstacle course that a player must complete. The course consists of (N) obstacles, each with a specified difficulty level. The player starts at the first obstacle and proceeds sequentially. The stress incurred moving from one obstacle to the next is given by the absolute difference between the difficulties of the current and the next obstacle, i.e. (|obstacle_{i+1} - obstacle_i|). For the course to be completed, the stress incurred between any consecutive obstacles must not exceed a given threshold (K). If any such difference is greater than (K), the course cannot be completed, and the output should be (-1). Otherwise, print the total accumulated stress over the course.

Input/Output Format: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout).

inputFormat

The first line contains two integers (N) (the number of obstacles) and (K) (the maximum allowable stress between two consecutive obstacles). The second line contains (N) space-separated integers, representing the difficulty levels of the obstacles in order.

outputFormat

Output a single integer: the total accumulated stress if the course can be completed without exceeding any intermediate stress threshold; otherwise, output (-1).## sample

5 10
1 3 6 10 8
11