Make it Smooth (Large)

Edit pixel values, delete pixels, or insert new ones at given costs so neighboring values differ by at most M for the lowest total price.

Medium7Dynamic programmingMathNo attempts yetTime limit5sMemory limit512 MB

Problem

You have a one dimensional array of NN pixels. Each pixel holds an integer value between 0 and 255, inclusive. The distance between two pixels is the absolute difference of their values.

You may perform each of the following operations any number of times, including zero.

  1. For a cost of DD, delete one pixel. The two pixels that were its neighbors become neighbors of each other.
  2. For a cost of II, insert one pixel with any value between 0 and 255 at any position: between two existing pixels, before the first pixel, or after the last pixel.
  3. Change the value of one pixel. The cost is the absolute difference between the old value and the new value.

An array is smooth if every pair of neighboring pixels has distance at most MM. Find the minimum total cost of a sequence of operations that makes the array smooth.

The empty array, the array with no pixels at all, counts as smooth.

Input

The first line contains the number of test cases TT. Each test case takes two lines. The first line contains DD, II, MM, and NN separated by spaces. The second line contains the pixel values a1,a2,,aNa_1, a_2, \dots, a_N from left to right.

Limits

  • Every number in the input is an integer.
  • 1T1001 \le T \le 100
  • 0D,I,M,ai2550 \le D, I, M, a_i \le 255
  • 1N1001 \le N \le 100

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the minimum cost of making that array smooth.

Notes

In the first sample case, lowering the 7 to a 3 costs 4 and is the cheapest option. In the second sample case, deleting is very expensive, so inserting pixels until the final array is [1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 50, 45, 40, 35, 30, 25, 20, 15, 10, 7] costs less.