Convex Sequence
Time limit2sMemory limit512 MB
Subtract 1 from elements of a sequence (N up to 50) so that it becomes convex, minimizing total subtractions.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
An integer sequence is convex when holds for every with . A sequence of length 1 or 2 is always convex.
For example, 7, 3, 4, 5, 7 and 4, 2, 1, 3 are convex, while 4, 3, 1, 2 and 5, 7, 3 are not.
You are given a sequence . One operation picks an index and replaces with , and no other operation is allowed. You may pick the same index several times, and an element may become negative. Write a program that finds the minimum number of operations that makes convex.
Input
The first line contains the size of the sequence, (). The second line contains the integers () of the sequence , separated by spaces.
Output
Print the minimum number of operations that makes convex on the first line.