Snow-covered path
InterviewTime limit1sMemory limit1024 MB
Given the snow depth on each meter of a path, find the total energy spent when each thrown load of snow adds a 1 cm layer to the meters ahead.
- Level
Medium4 of 10
- Topics
- Prefix sum, Simulation
- Solved
- No attempts yet
Problem
Latvia has had unusually frequent snow this winter. Last night Mārtiņš cleared the path leading to his house. This morning, when he woke up and looked out the window, the path was covered in snow again.
The path is a straight segment meters long. The snow covering it is uneven. The first meter is covered by snow centimeters deep, the second meter by centimeters, and so on, up to the last meter, which is covered by centimeters.
Mārtiņš starts clearing the path from the first meter. He scoops up all the snow there with a shovel and throws it up and away from himself. The snow falls back onto the path and covers the nearest few meters with a layer 1 centimeter deep. Consider this example.

Figure 1. A 3 centimeter layer of snow spreads over a 3 meter interval.
After clearing the first meter, Mārtiņš moves on to the second. He throws the snow there up, and it again covers the nearest few meters with a 1 centimeter layer. Then he clears the third meter, the fourth, and so on, until no snow is left on the path. The example continues like this.

Figure 2. Clearing the path. Snow that falls beyond the path stays where it lands.
Each time he throws up centimeters of snow, Mārtiņš spends energy units. In the example above, he spends energy units in total.
Input
The first line contains the integer , the length of the path.
The second line contains integers separated by spaces, , which describe the depth of the snow covering the path.
Output
Print one number, the amount of energy Mārtiņš spends.