#K7256. Minimum Total Cost to Increase Flower Heights

    ID: 33780 Type: Default 1000ms 256MiB

Minimum Total Cost to Increase Flower Heights

Minimum Total Cost to Increase Flower Heights

You are given N flowers with initial heights. Your task is to determine the minimum total cost required to increase the heights such that the sequence of heights becomes non-decreasing. In other words, for each i from 2 to N, the condition height[i] \geq height[i-1] must be satisfied.

If a flower's height is less than the previous flower's height, you must increase it. The cost to increase a flower's height is equal to the difference between the previous height and the current height, i.e. $\max(0, \text{height}[i-1]-\text{height}[i])$.

Implement a program that reads the number of flowers and their initial heights from the standard input and prints the minimum total cost required to achieve a non-decreasing height sequence.

inputFormat

The input consists of two lines:

  • The first line contains an integer N representing the number of flowers.
  • The second line contains N space-separated integers, representing the initial heights of the flowers.

It is guaranteed that N is at least 1.

outputFormat

Output a single integer representing the minimum total cost required to make the sequence of flower heights non-decreasing.

## sample
5
4 2 3 1 5
6