#P1849D. Array Painting
Array Painting
No submission language available for this problem.
Description
You are given an array of integers, where each integer is either , , or . Initially, each element of the array is blue.
Your goal is to paint each element of the array red. In order to do so, you can perform operations of two types:
- pay one coin to choose a blue element and paint it red;
- choose a red element which is not equal to and a blue element adjacent to it, decrease the chosen red element by , and paint the chosen blue element red.
What is the minimum number of coins you have to spend to achieve your goal?
The first line contains one integer ().
The second line contains integers ().
Print one integer — the minimum number of coins you have to spend in order to paint all elements red.
Input
The first line contains one integer ().
The second line contains integers ().
Output
Print one integer — the minimum number of coins you have to spend in order to paint all elements red.
Note
In the first example, you can paint all elements red with having to spend only one coin as follows:
- paint the -nd element red by spending one coin;
- decrease the -nd element by and paint the -st element red;
- decrease the -nd element by and paint the -rd element red.
In the second example, you can paint all elements red spending only two coins as follows:
- paint the -th element red by spending one coin;
- decrease the -th element by and paint the -rd element red;
- paint the -st element red by spending one coin;
- decrease the -rd element by and paint the -nd element red.