#K54857. Total Elevation Gain
Total Elevation Gain
Total Elevation Gain
You are given an integer n and a sequence of n integers representing the heights of segments. Your task is to calculate the total elevation gain. The elevation gain between two adjacent segments is defined as the positive difference between their heights.
In mathematical terms, if the heights are given by \(h_1, h_2, \ldots, h_n\), then you need to compute:
$$\sum_{i=2}^{n} \max(0, h_i - h_{i-1})$$where \(\max(0, h_i - h_{i-1})\) represents the elevation gain from the \( (i-1)\)-th segment to the \(i\)-th segment.
inputFormat
The first line of input contains a single integer n
(the number of segments).
The second line contains n
space-separated integers, where the \(i\)-th integer represents the height of the \(i\)-th segment.
outputFormat
Output a single integer: the total elevation gain calculated by summing all the positive differences between consecutive segments.
## sample5
1 5 3 6 4
7