CF205B.Little Elephant and Sorting

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Little Elephant loves sortings.

He has an array aa consisting of nn integers. Let's number the array elements from 1 to nn , then the ii -th element will be denoted as aia_{i} . The Little Elephant can make one move to choose an arbitrary pair of integers ll and rr (1<=l<=r<=n)(1<=l<=r<=n) and increase aia_{i} by 11 for all ii such that l<=i<=rl<=i<=r .

Help the Little Elephant find the minimum number of moves he needs to convert array aa to an arbitrary array sorted in the non-decreasing order. Array aa , consisting of nn elements, is sorted in the non-decreasing order if for any ii (1<=i<n) ai<=ai+1a_{i}<=a_{i+1} holds.

输入格式

The first line contains a single integer nn (1<=n<=105)(1<=n<=10^{5}) — the size of array aa . The next line contains nn integers, separated by single spaces — array aa (1<=ai<=109)(1<=a_{i}<=10^{9}) . The array elements are listed in the line in the order of their index's increasing.

输出格式

In a single line print a single integer — the answer to the problem.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

输入输出样例

  • 输入#1

    3
    1 2 3
    

    输出#1

    0
    
  • 输入#2

    3
    3 2 1
    

    输出#2

    2
    
  • 输入#3

    4
    7 4 1 47
    

    输出#3

    6
    

说明/提示

In the first sample the array is already sorted in the non-decreasing order, so the answer is 00 .

In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).

In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].

首页