Clearing the Track
Time limit1sMemory limit1024 MB
Given a circular track where each pickup reverses direction, find the total distance Dalgu walks until all trash is removed.
- Level
Medium5 of 10
- Topics
- Math, Simulation, Array
- Solved
- No attempts yet
Problem
The day after the DGIST sports day, Dalgu decides to clean up a circular running track of length that is full of trash.
The track has zones numbered 1 to clockwise around its circumference, and each zone is 1 unit apart. Zone holds pieces of trash. Dalgu wants to remove all the trash from every zone, and to pass the time, Dalgu removes it in the following way.
- Dalgu starts at zone 1, which has no trash, and moves clockwise.
- If the zone reached has no trash, Dalgu keeps moving in the same direction.
- If the zone reached has trash, Dalgu removes one piece from that zone and reverses the direction of movement.
- Once all the trash in every zone is removed, Dalgu finishes and stops moving.
Input
The first line contains the number of zones . ()
The second line contains the amounts of trash in each zone, separated by spaces. (, )
Output
Print the total distance Dalgu travels while following the rules above.