luogu#P5019. [NOIP 2018 提高组] 铺设道路

[NOIP 2018 提高组] 铺设道路

Background

NOIP 2018 Senior D1T1.

Problem Description

Chunchun is a road engineer responsible for paving a road of length nn.

The main task of paving is to fill the sunken ground surface. The whole road can be seen as nn consecutive segments connected end to end. Initially, the sinking depth of the ii-th segment is did_i.

Each day, Chunchun can choose a continuous interval [L,R][L,R] and fill every segment in this interval, reducing its sinking depth by 11. When choosing an interval, it must be guaranteed that, before filling, the sinking depth of every segment in the interval is not 00.

Chunchun wants you to help design a plan that turns the sinking depths of the entire road into 00 in the shortest time.

Input Format

The input file contains two lines. The first line contains an integer nn, representing the length of the road. The second line contains nn integers separated by a single space; the ii-th integer is did_i.

Output Format

The output file contains only one integer, which is the minimum number of days needed to complete the task.

6   
4 3 2 5 3 5 

9

Hint

【Sample Explanation】

One possible optimal plan is to choose, in order: [1,6][1,6], [1,6][1,6], [1,2][1,2], [1,1][1,1], [4,6][4,6], [4,4][4,4], [4,4][4,4], [6,6][6,6], [6,6][6,6].

【Constraints】

For 30%30\% of the testdata, 1n101 \le n \le 10. For 70%70\% of the testdata, 1n10001 \le n \le 1000. For 100%100\% of the testdata, 1n1000001 \le n \le 100000, 0di100000 \le d_i \le 10000.

Translated by ChatGPT 5