A trail ranger's handheld monitor keeps a single running log while a hiker moves along a ridge trail. Every time the hiker passes one of the numbered beacon posts staked along the trail, the monitor writes a 0 into the log to mark that moment. In between two consecutive beacon markers, the monitor instead writes one entry for every elevation-gain reading it picks up along that stretch of trail. The log always starts and ends with a beacon marker, and it is guaranteed that at least one elevation-gain reading was recorded between any two consecutive beacon markers.
To build a clean trip report, collapse every stretch between two consecutive beacon markers into a single number: the sum of all elevation-gain readings recorded during that stretch. The beacon markers themselves carry no elevation information and must not appear in the report.
n, the number of entries in the log.n space-separated integers a_1 ... a_n, the log in the order it was recorded.Print the collapsed stretch totals, in the order the stretches occurred, separated by single spaces, on one line.
Example 1
Input
8 0 3 1 0 4 5 2 0
Expected
4 11
Explanation
The log has beacon markers at positions 1, 4, and 8. Between the first two markers the readings are 3 and 1, summing to 4. Between the last two markers the readings are 4, 5, and 2, summing to 11. Reporting the two stretch totals in order gives "4 11".
Example 2
Input
5 0 9 0 3 0
Expected
9 3
Explanation
There are three beacon markers, creating two single-reading stretches: [9] and [3]. Their sums are 9 and 3, so the report is "9 3".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →