すべてのチャレンジ
簡単 0件解決済み 正答率0%

Make an Array Nondecreasing

Find the fewest increments needed so each value is at least the previous one.

One move increases one array value by 1. Make the array nondecreasing: each value must be greater than or equal to the one immediately before it. You may not decrease values or reorder them.

Implement increasing_array(numbers).

Input

numbers: The values in their original order.

Output

Return the minimum total number of unit increments.

制約

1 ≤ length ≤ 200,000. Each value is between 1 and 1,000,000,000. Use a signed 64-bit result.

例

例 1
入力:
numbers = [4, 1, 3]

期待される出力:
expected = 4
入力:
std::vector<long long> numbers = {4, 1, 3};

期待される出力:
long long expected = 4;
入力:
long[] numbers = new long[] {4, 1, 3};

期待される出力:
long expected = 4;

解説

Raise 1 to 4 using three moves and 3 to 4 using one move. The result [4, 4, 4] uses four moves.

例 2
入力:
numbers = [1, 2, 2, 5]

期待される出力:
expected = 0
入力:
std::vector<long long> numbers = {1, 2, 2, 5};

期待される出力:
long long expected = 0;
入力:
long[] numbers = new long[] {1, 2, 2, 5};

期待される出力:
long expected = 0;

解説

Every value is already at least the preceding value; equal neighbors are allowed.

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。