All problems

Find the Highest Altitude

Easy · prefix sum

A hiker sets off from a trail head at altitude 0. The trail has a number of stops, and the list gain records the climb between neighbouring stops: gain[i] is how much the altitude changes when the hiker walks from stop i to stop i + 1. Stop 0 is the trail head itself. A negative number means the hiker went downhill.

Return the highest altitude the hiker stands at, counting every stop including the trail head.

Examples

Example 1

Input: gain = [-5, 1, 5, 0, -7]

Output: 1

The altitudes at the stops are 0, -5, -4, 1, 1, -6, so the highest is 1.

Example 2

Input: gain = [-4, -3, -2, -1, 4, 3, 2]

Output: 0

The altitudes are 0, -4, -7, -9, -10, -6, -3, -1. The hiker never climbs back up to the start, so the trail head is the highest point.

Example 3

Input: gain = [10, 20, 30]

Output: 60

Constraints