Meticulous smoothing
InterviewTime limit1sMemory limit1024 MB
Given a plank's thickness at n positions, shave off 1 micrometer at a time so that neighboring thicknesses differ by at most 1; find the minimum number of shaves.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Greedy, Array, Prefix sum
- Solved
- No attempts yet
Problem
The arts and crafts teacher looks at the beautiful plank you made in woodshop and fixes you with a stern stare. "This is not smooth enough! Use more sandpaper!"
Your plank is cm long, and to prove his point the teacher measured its thickness at different locations. He demands that the thickness differ by no more than micrometer between any two consecutive measured locations. Each time you use the sandpaper at a particular location, it shaves off micrometer of wood. How many times do you need to use the sandpaper?
Input
The first line contains a single integer , the length of your plank. The second line contains space-separated integers , the thickness of your plank ( for every ).
Output
Output a single integer, the minimum number of times you need to use the sandpaper (assuming the sandpaper touches only one location at a time).