Pool
InterviewTime limit6sMemory limit1024 MB
Given N distance readings from the shore, find the minimum number of pool lengths Bajtek must have swum to produce them.
- Level
Medium5 of 10
- Topics
- Greedy, Implementation
- Solved
- No attempts yet
Problem
Bajtek finally bought a waterproof smartwatch. He went straight to the pool to check that it works. The watch is rather strange. At various moments it recorded how far Bajtek was from the start of the pool, that is, from the shore where he started. Bajtek read the sequence of recorded distances, but he does not understand how to interpret it. He wants to know the minimum number of pool lengths he could have swum.
You may assume that Bajtek turned around only after reaching the far end of the pool, and that he started and finished at the shore, even if the watch did not record this. Bajtek may sometimes stop to rest, not necessarily at the edge of the pool.
Input
The first line contains one natural number , , the number of readings on the watch. The second and last line contains integers , , the successive distances from a fixed shore of the pool. At least one of the numbers is not zero.
Output
Print one integer, the minimum number of pool lengths Bajtek swam.