Growing Vegetables is Fun 4
Time limit1sMemory limit512 MB
Given N heights in a row, each operation adds 1 to a contiguous interval; find the minimum number of operations so the final sequence strictly increases then strictly decreases.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Array, Math
- Solved
- No attempts yet
Problem
Bitaro likes gardening. He is now growing plants called Biba-herbs in the garden. There are N Biba-herbs in the garden, planted in a line from the west to the east. The Biba-herbs are numbered from 1 to N from the west to the east. Now, the height of the Biba-herb i (1 ≤ i ≤ N) is Ai.
Due to the breed improvement, if Bitaro waters a Biba-herb once, then its height increases by 1. Since he wants to decorate the garden, he will water the Biba-herbs several times so that the following condition is satisfied.
- After Bitaro finishes watering, let Bi be the height of the Biba-herb i. Then, there exists an integer k (1 ≤ k ≤ N) such that Bj < Bj+1 holds for every 1 ≤ j ≤ k − 1, and Bj > Bj+1 holds for every k ≤ j ≤ N − 1.
However, Bitaro is not good at watering. When he waters Biba-herbs, he can only water consecutive Biba-herbs in an interval. In other words, he chooses integers L and R (1 ≤ L ≤ R ≤ N) and waters the Biba-herbs L, L + 1, . . . , R.
Bitaro wants to minimize the number of times of watering.
Write a program which, given the number of Biba-herbs and their current heights, calculates the minimum number of times of watering so that the above condition is satisfied.
Input
Read the following data from the standard input. Given values are all integers.
N
A1 · · · AN
Output
Write one line to the standard output. The output should contain the minimum number of times of watering.
Constraints
- 2 ≤ N ≤ 200 000.
- 1 ≤ Ai ≤ 1 000 000 000 (1 ≤ i ≤ N).