#K44837. Minimum Absolute Difference Sum

    ID: 27620 Type: Default 1000ms 256MiB

Minimum Absolute Difference Sum

Minimum Absolute Difference Sum

Given \(T\) test cases, each test case consists of an array of \(N\) integers and an integer \(K\). You are allowed to reorder the array arbitrarily and, for each adjacent pair \((a, b)\) in the reordered array, you can opt to replace the left element \(a\) with \(K+1\) if doing so reduces the cost. For each adjacent pair, the cost is defined as:

[ \text{cost} = \min\big(|a - b|, ; |(K+1) - a|\big) ]

The task is to determine the minimum possible total cost, which is the sum of these costs over all adjacent pairs in the array.

Note: When the array contains a single element, the cost is \(0\).

inputFormat

The input begins with an integer \(T\) indicating the number of test cases. Each test case consists of two lines:

  • The first line contains two space‐separated integers: \(N\) (the number of integers in the array) and \(K\).
  • The second line contains \(N\) space‐separated integers representing the array elements.

outputFormat

For each test case, output a single line containing the minimum possible sum of absolute differences between adjacent elements after applying the allowed operations.

## sample
1
4 10
11 5 3 8
8

</p>