#P4331. Minimize Absolute Difference Sum with Increasing Sequence

    ID: 17577 Type: Default 1000ms 256MiB

Minimize Absolute Difference Sum with Increasing Sequence

Minimize Absolute Difference Sum with Increasing Sequence

Given an integer sequence \(a_1, a_2, \ldots, a_n\), find a strictly increasing sequence \(b_1 < b_2 < \cdots < b_n\) such that the sum

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

is minimized. The sequence \(b\) can be chosen arbitrarily (with the only constraint being that it is strictly increasing).

Note: The input consists of the integer \(n\) (the number of elements) followed by \(n\) integers representing the sequence \(a\). The output is a single integer that represents the minimum sum of the absolute differences \(|a_i - b_i|\).

inputFormat

The first line contains an integer \(n\) (the number of elements in the sequence). The second line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\).

outputFormat

Output a single integer, the minimum possible value of \(|a_1 - b_1| + |a_2 - b_2| + \cdots + |a_n - b_n|\) for some strictly increasing sequence \(b_1 < b_2 < \cdots < b_n\).

sample

3
10 1 5
10

</p>