#C11198. Minimum Operations to Achieve Exactly One Peak

    ID: 40487 Type: Default 1000ms 256MiB

Minimum Operations to Achieve Exactly One Peak

Minimum Operations to Achieve Exactly One Peak

You are given a sequence of n integers, where \(3 \le n \le 1000\) and each integer is between \(1\) and \(10^9\) (inclusive). Your task is to determine the minimum number of operations (increments or decrements by 1) needed so that the sequence contains exactly one peak.

A peak is defined as an element \(a_i\) (with \(1 < i a_{i-1}\) and \(a_i > a_{i+1}\). If the sequence already has exactly one peak, no operations are needed (answer is 0). If there is no peak, you must perform a minimal number of operations to create one. However, if the sequence initially has more than one peak, transforming it to have exactly one peak is considered impossible and you should output -1.

Note: In this problem, you are only allowed to increment or decrement the values. For the purpose of this challenge, assume the most efficient transformation is achieved by modifying a single element in the best possible way.

inputFormat

The first line contains an integer \(n\) (the length of the sequence). The second line contains \(n\) space-separated integers representing the sequence.

\(3 \le n \le 1000\) and \(1 \le a_i \le 10^9\).

outputFormat

Output a single integer: the minimum number of operations required to transform the sequence so that it has exactly one peak, or -1 if it is not possible.

## sample
5
1 2 3 2 1
0

</p>