A meteorology team releases a weather balloon fitted with an altimeter that logs the change in altitude between consecutive checkpoints during its ascent. The balloon launches from ground level, which is defined as altitude 0. At each checkpoint the altimeter records how much the altitude changed relative to the previous checkpoint (a positive value means it climbed, a negative value means it descended). Given this list of altitude changes, report the single highest altitude the balloon reaches at any checkpoint during the entire flight, counting the launch point itself as a checkpoint.
Line 1: an integer n — the number of recorded altitude changes.
Line 2: n space-separated integers d_1 d_2 ... d_n — the altitude change recorded at each checkpoint (checkpoint i's altitude equals the launch altitude plus d_1 + d_2 + ... + d_i).
A single integer: the maximum altitude reached at any checkpoint from the launch (altitude 0) through the n-th checkpoint.
Example 1
Input
5 3 -2 4 -1 2
Expected
6
Explanation
Running altitude after each checkpoint: 0 (launch), 3, 1, 5, 4, 6. The highest value reached at any checkpoint is 6, achieved after the fourth checkpoint (deltas 3, -2, 4, -1 summed).
Example 2
Input
7 -4 -3 -2 -1 4 3 2
Expected
0
Explanation
Running altitude after each checkpoint: 0, -4, -7, -9, -10, -6, -3, -1. Every checkpoint is below the launch altitude, so the highest altitude reached is the launch altitude itself, 0.
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 →