#P1969. [NOIP 2013 提高组] 积木大赛

    ID: 915 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>模拟贪心20132018递归NOIp 提高组

[NOIP 2013 提高组] 积木大赛

Description

Chun-Chun Kindergarten is holding its annual "Building Blocks Contest." This year, the task is to construct a building of width nn, which can be regarded as composed of nn blocks each of width 11. The final height of the ii-th block should be hih_i.

Before construction starts, there are no blocks (equivalently, there are nn blocks of height 00). In each operation, the children may choose a continuous interval [L,R][L, R], and then increase by 11 the height of every block from the LL-th to the RR-th (inclusive).

Xiao M is a clever child and quickly figured out an optimal strategy that minimizes the number of operations. However, she is not keen on doing it herself, so she asks you to implement this strategy and compute the minimum number of operations.

Input Format

There are two lines. The first line contains an integer nn, the width of the building.
The second line contains nn integers, where the ii-th integer is hih_i.

Output Format

Output the minimum number of operations required to complete the construction.

5
2 3 4 1 2
5

Hint

Sample explanation:
One optimal sequence is to choose: [1,5][1, 5], [1,3][1, 3], [2,3][2, 3], [3,3][3, 3], [5,5][5, 5].

Constraints:

  • For 30%30\% of the testdata, 1n101 \leq n \leq 10.
  • For 70%70\% of the testdata, 1n10001 \leq n \leq 1000.
  • For 100%100\% of the testdata, 1n1000001 \leq n \leq 100000, 0hi100000 \leq h_i \leq 10000.

Translated by ChatGPT 5