Codeforces Round 129 (Div. 2)
B
Little Elephant and Sorting
Codeforces
1400
原题
← AC →
翻译中…

The Little Elephant loves sortings.

翻译中…

He has an array $*a*$ consisting of $*n*$ integers. Let's number the array elements from 1 to $*n*$, then the $*i*$-th element will be denoted as $*a*_{*i*}$. The Little Elephant can make one move to choose an arbitrary pair of integers $*l*$ and $*r*$ $(1 ≤ *l* ≤ *r* ≤ *n*)$ and increase $*a*_{*i*}$ by $1$ for all $*i*$ such that $*l* ≤ *i* ≤ *r*$.

翻译中…

Help the Little Elephant find the minimum number of moves he needs to convert array $*a*$ to an arbitrary array sorted in the non-decreasing order. Array $*a*$, consisting of $*n*$ elements, is sorted in the non-decreasing order if for any $*i*$ $(1 ≤ *i* < *n*)$ $*a*_{*i*} ≤ *a*_{*i* + 1}$ holds.

翻译中…

Input

翻译中…

The first line contains a single integer $*n*$ $(1 ≤ *n* ≤ 10^{5})$ — the size of array $*a*$. The next line contains $*n*$ integers, separated by single spaces — array $*a*$ $(1 ≤ *a*_{*i*} ≤ 10^{9})$. The array elements are listed in the line in the order of their index's increasing.

翻译中…

Output

翻译中…

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.

翻译中…

Note

翻译中…

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

翻译中…

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].

翻译中…

样例

Input #1
3
1 2 3
Output #1
0
InputCopy OutputCopy
Input #2
3
3 2 1
Output #2
2
InputCopy OutputCopy
Input #3
4
7 4 1 47
Output #3
6
InputCopy OutputCopy
题目信息
题号
205B
时间限制
0.5 s
内存限制
256 MB
标签
暴力
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。