#K37682. Minimum Steps to Make Array Non-decreasing

    ID: 26031 Type: Default 1000ms 256MiB

Minimum Steps to Make Array Non-decreasing

Minimum Steps to Make Array Non-decreasing

You are given an array of N integers. In one step, you can increase the value of any element in the array. Your task is to determine the minimum number of steps required to make the array non-decreasing.

An array is non-decreasing if for every index \(i\)\, (where \(2 \le i \le N\)), the following holds: \[ \texttt{arr}[i] \geq \texttt{arr}[i-1] \]

If \(\texttt{arr}[i] < \texttt{arr}[i-1]\), you must increment \(\texttt{arr}[i]\) by \(\texttt{arr}[i-1] - \texttt{arr}[i]\) to make it equal to \(\texttt{arr}[i-1]\). The total number of increments performed is the answer.

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  • The first line contains a single integer \(N\), the size of the array.
  • The second line contains \(N\) space-separated integers representing the elements of the array.

outputFormat

Output a single integer to standard output (stdout), which is the minimum number of steps required to make the array non-decreasing.

## sample
5
3 2 5 1 7
5