#K45157. Minimum Irregularity of Painting Arrangement

    ID: 27691 Type: Default 1000ms 256MiB

Minimum Irregularity of Painting Arrangement

Minimum Irregularity of Painting Arrangement

You are given n paintings, each with a style rating. Your goal is to rearrange the paintings such that the total irregularity is minimized. The irregularity of an arrangement is calculated as the sum of the absolute differences between the style ratings of consecutive paintings.

Formally, if the paintings are arranged in an order \(b_1, b_2, \dots, b_n\), the irregularity is defined as:

$$\sum_{i=2}^{n} |b_i - b_{i-1}|$$

You need to determine the minimal possible irregularity.

inputFormat

The first line of input contains an integer n denoting the number of paintings.

The second line contains n space-separated integers, where the i-th integer represents the style rating of the i-th painting.

outputFormat

Output a single integer, which is the minimum possible irregularity of the arrangement.

## sample
4
1 3 2 5
4

</p>