#K62817. Minimum Operations to Make Array Non-Decreasing

    ID: 31616 Type: Default 1000ms 256MiB

Minimum Operations to Make Array Non-Decreasing

Minimum Operations to Make Array Non-Decreasing

Given an array of integers, determine the minimum number of operations required to make the array non-decreasing. In one operation, you can increment or decrement any element by 1. The resulting array should satisfy \(a_1 \le a_2 \le \cdots \le a_n\).

For example, for the array [4, 2, 3, 1, 5], the minimum number of operations required is 6.

inputFormat

The input is given via stdin and consists of two lines. The first line contains a single integer (n) (the number of elements). The second line contains (n) space-separated integers representing the array elements.

outputFormat

Output via stdout a single integer representing the minimum number of operations required to convert the array into a non-decreasing sequence.## sample

5
4 2 3 1 5
6