Increasing Array
Problem Source
Problem Statement
You are given an array of $n$ integers. You want to modify the array so that it is increasing, i.e., every element is at least as large as the previous element.
On each move, you may increase the value of any element by one. What is the minimum number of moves required?
Solution
1
2
3
4
5
6
7
8
9
10
11
n = int(input())
a = list(map(int, input().split()))
result = 0
for i in range(1, n):
if a[i] < a[i - 1]:
result += a[i - 1] - a[i]
a[i] = a[i - 1]
print(result)
This post is licensed under CC BY 4.0 by the author.