Noip 2013 block game

Source: Internet
Author: User
Description

Spring kindergarten held the annual "building block competition ". This year's competition is about building a building with a width of n. The building can be regarded as composed of N blocks with a width of 1 ?? The final height of block blocks must be hi.

No building blocks (N blocks with a height of 0 can be considered) before the building starts ). Next, each operation, the children can select a continuous interval [L, R], and then between the L Block and the R block (including the L Block and the R Block) the height of all building blocks increases by 1.

John M is a smart child. She quickly came up with the best strategy to build a building, minimizing the number of operations required for the building. However, she is not a very diligent child, so I would like to ask you to implement this strategy and find the minimum number of operations.

Format input format

The input contains two rows. The first row contains an integer N, indicating the width of the building.

The second row contains N integers, and the I-th integer is hi.

Output Format

Only one row, that is, the minimum number of operations required for building.

Example 1 input 1

5
2 3 4 1 2

Sample output 1

5

Prompt

One of the feasible best solutions is to select[] [] [] [] []

For 30% of data, 1 ≤ n ≤ 10;
For 70% of data, 1 ≤ n ≤ 1000;
For 100% of data, 1 ≤ n ≤ 100000,0 ≤ Hi ≤ 10000.

Solution:

I wanted to write it with rmq, but it's a pity that it's down,

After reading the problem on the internet, I went through greedy O (N). I ......

Find the minimum value of the entire segment, and then find a bunch of non-decreasing intervals. The maximum value of each interval is added and the minimum value of the entire segment is equal to orz.

Noip 2013 block game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.