Kim Kangsan

Time limit3sMemory limit128 MB

Summary
Given fixed first and last pile heights, find the minimum total bricks added or removed to any middle pile so adjacent height differences stay within d.
Level

Medium6 of 10

Topics
Dynamic programming, Array, Greedy
Solved
No attempts yet

Statement

Kim Kangsan is an artificial mountain built by stacking bricks, where people come to practice rock climbing. Its slope is so steep, however, that beginners struggle to climb it. The caretaker, Sanggeun, wants to reshape it to be gentler.

Kim Kangsan consists of nn brick piles in a row; the ii-th pile from the left has hih_i bricks stacked on it. The height difference between two adjacent piles is ∣hi+1−hi∣|h_{i+1} - h_i|. Sanggeun wants every adjacent pair of piles to have a height difference of at most dd.

Sanggeun may add bricks to or remove bricks from any pile, but he cannot change the number of bricks in the first pile or the last pile. Adding or removing a single brick costs 11 unit of effort, and Sanggeun wants to minimize the total effort.

Given the height of every pile, find the minimum number of bricks that must be added or removed so that every adjacent height difference is at most dd. If it is impossible to satisfy the condition, print impossible.

Input

The first line contains the number of test cases TT. (1≤T≤1001 \le T \le 100)

Each test case consists of two lines. The first line contains the number of piles nn and the maximum allowed height difference dd, separated by a space. (2≤n≤1002 \le n \le 100, 0≤d≤1090 \le d \le 10^9) The second line contains the number of bricks on each pile, h1,h2,…,hnh_1, h_2, \dots, h_n, separated by spaces. (0≤hi≤1090 \le h_i \le 10^9)

Output

For each test case, print on its own line the minimum number of bricks that must be added or removed so that every adjacent height difference is at most dd. If the condition cannot be satisfied, print impossible.

Examples1

  1. Example 1

    Input
    3
    10 2
    4 5 10 6 6 9 4 7 9 8
    3 1
    6 4 0
    4 2
    3 0 6 3
    
    Expected output
    6
    impossible
    4