#C7704. Minimum Operations to Make Array Non-Decreasing

    ID: 51605 Type: Default 1000ms 256MiB

Minimum Operations to Make Array Non-Decreasing

Minimum Operations to Make Array Non-Decreasing

You are given an array of integers of length N. Your task is to determine the minimum number of operations required to make the array non-decreasing. In one operation, you can increase an element of the array. The strategy is to traverse the array from left to right, and whenever an element is found that is less than its previous element, add the difference to a counter and update the current element so that it matches the previous one.

Mathematically, for each index \(i\) (where \(1 \leq i < N\)), if \(A[i] < A[i-1]\), then the number of operations required is increased by \(A[i-1]-A[i]\), and the element \(A[i]\) is set to \(A[i-1]\). The answer is the total sum of these differences.

inputFormat

The first line contains a single integer N representing the length of the array. The second line contains N space‐separated integers representing the array elements.

outputFormat

Output a single integer representing the minimum number of operations needed to transform the array into a non-decreasing array.

## sample
5
1 5 3 6 7
2