#K35217. Minimum Increments to Make Array Non-decreasing

    ID: 25483 Type: Default 1000ms 256MiB

Minimum Increments to Make Array Non-decreasing

Minimum Increments to Make Array Non-decreasing

You are given an array of integers. Your task is to determine the minimum number of increments needed to transform the given array into a non-decreasing array. In one move, you can increment any element of the array by one.

A non-decreasing array satisfies the condition:

a1a2ana_1 \leq a_2 \leq \cdots \leq a_n

For any index i (where 2 \leq i \leq n), if a_i is less than a_{i-1}, you must increment a_i until a_i \geq a_{i-1}. The cost of each increment is 1. Determine the minimum total cost needed to make the array non-decreasing.

Example:

Input: [4, 2, 3, 1, 5]
Output: 6

Explanation: For the array [4, 2, 3, 1, 5], we need to increment the second element from 2 to 4 (cost 2) and the fourth element from 1 to 3 (cost 2) and then adjust properly to obtain the sequence [4, 4, 4, 4, 5] (or a similar valid non-decreasing sequence). The total cost here is 6.

inputFormat

The first line contains a single integer n, representing the number of elements in the array.

The second line contains n space-separated integers, representing the elements of the array.

outputFormat

Output a single integer: the minimum number of increments required to make the array non-decreasing.

## sample
5
4 2 3 1 5
6