#C6380. Minimize Differences in Magic Stones Arrangement

    ID: 50134 Type: Default 1000ms 256MiB

Minimize Differences in Magic Stones Arrangement

Minimize Differences in Magic Stones Arrangement

Moana has a collection of magic stones, each numbered with a unique magical power index. The stones are arranged sequentially but some positions are missing, marked by \(-1\). Moana wishes to fill these gaps optimally so that the total sum of the absolute differences between adjacent stones is minimized.

Formally, if the final arrangement is \(a_1, a_2, \ldots, a_n\), the objective is to minimize the value:

[ \sum_{i=2}^{n} |a_i - a_{i-1}| ]

You are given the number of stones \(n\) (with \(2 \le n \le 1000\)) and an array representing the stones arrangement where a value of \(-1\) indicates a gap. There is at least one stone with a valid magical power index (between 0 and 1000), and these valid values ensure that a continuous sequence can be formed once the gaps are filled.

Your task is to compute the minimum possible sum of the differences between adjacent stones after optimally filling the gaps.

inputFormat

The first line contains an integer \(n\) — the number of stones. The second line contains \(n\) integers representing the arrangement of stones, where each integer is either a non-negative number (the magical power index) or \(-1\) indicating a gap. The values are separated by spaces.

outputFormat

Output a single integer representing the minimum possible sum of the absolute differences between adjacent stones after filling in the gaps optimally.

## sample
5
2 -1 3 -1 4
2

</p>