#K55412. Minimum Operations to Make Array Non-Decreasing
Minimum Operations to Make Array Non-Decreasing
Minimum Operations to Make Array Non-Decreasing
You are given an integer n and an array of n integers. Your task is to determine the minimum total increment operations required to transform the array into a non-decreasing sequence.
In one operation, you can choose an element that is smaller than its previous element and increase it to match the previous element. In other words, for every index i (where 1 \leq i < n), if arr[i] < arr[i-1], you can add arr[i-1] - arr[i] to arr[i]. The total number of operations is the sum of all such increments.
This can be formulated mathematically as follows:
$$\text{operations} = \sum_{i=1}^{n-1} \max(0, arr[i-1] - arr[i])$$
inputFormat
The first line contains an integer n ($1 \leq n \leq 10^5$), representing the number of elements in the array.
The second line contains n space-separated integers, which represent the elements of the array. The values of the integers can be negative, zero, or positive.
outputFormat
Output a single integer denoting the minimum total number of operations required to make the array non-decreasing.
## sample4
1 2 3 4
0
</p>