#P11119. Minimizing Road Instability via Antenna Replacement
Minimizing Road Instability via Antenna Replacement
Minimizing Road Instability via Antenna Replacement
In this problem, you are given an array of n antennas placed along a road, where each antenna has a given power. The instability of the road coverage F is defined as the sum of pairwise instabilities between all antennas. Mathematically,
$$F = \sum_{s=1}^{n-1} \sum_{t=s+1}^{n} |a_s - a_t|.$$
The road operator has one spare antenna with a power of x. In order to reduce the instability, you are allowed to replace at most one existing antenna with the spare antenna (its power becomes x).
Your task is to determine the minimum possible value of F after performing at most one replacement.
Note: If no replacement yields an improvement, you may choose not to replace any antenna.
inputFormat
The input consists of two lines:
- The first line contains two integers n and x, where n is the number of antennas (1 ≤ n) and x is the power of the spare antenna.
- The second line contains n space-separated integers representing the power values of the antennas.
outputFormat
Output a single integer, which is the minimum possible instability F after replacing at most one antenna with the spare antenna of power x.
sample
3 0
3 1 2
4