CF205B.Little Elephant and Sorting

普及/提高-

通过率:0%

时间限制:0.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

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.

小象喜欢排序。

他有一个由 nn 个整数组成的数组 aa。我们将数组元素从 11 编号到 nn,则第 ii 个元素记为 aia_i。小象每次操作可以任选一对整数 ll 和 rr(满足 1≤l≤r≤n1 \leq l \leq r \leq n),并对所有满足 l≤i≤rl \leq i \leq r 的下标 ii,将 aia_i 增加 11。

请帮助小象找出:将数组 aa 变为任意一个非递减排序数组所需的最少操作次数。一个含 nn 个元素的数组 aa 被称为非递减排序的,当且仅当对任意 ii(1≤i<n1 \leq i < n)均满足 ai≤ai+1a_i \leq a_{i+1}。

输入格式

The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ a__i ≤ 109). The array elements are listed in the line in the order of their index's increasing.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)—— 数组 aa 的大小。
下一行包含 nn 个整数,以单个空格分隔 —— 数组 aa(1≤ai≤1091 \leq a_i \leq 10^9)。
数组元素按其下标递增的顺序在该行中列出。

输出格式

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.

在一行中输出一个整数——即该问题的答案。

请注意,在 C++ 中读取或写入 64 位整数时,请勿使用 %lld 说明符。推荐使用 cin、cout 流,或 %I64d 说明符。

输入输出样例

  • 输入#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 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].

在第一个样例中,数组已经按非递减顺序排好序,因此答案为 0。

在第二个样例中,你需要执行两次操作:第一次将第 2 个到第 3 个元素全部增加(操作后数组变为:[3, 3, 2]),第二次仅增加最后一个元素(操作后数组变为:[3, 3, 3])。

在第三个样例中,你至少需要进行 6 步操作。一种可能的操作序列是:(2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3)。操作完成后,数组变为 [7, 7, 7, 47]。

输入解题思路,AI测评打分。不知道怎么写?

首页