Climbers
Time limit0.8sMemory limit1024 MB
Given a mountain range as altitudes that start and end at 0, find the minimum total effort Alice and Bob need to meet at the same altitude.
- Level
Medium7 of 10
- Topics
- Array, Brute force, Dynamic programming
- Solved
- No attempts yet
Problem
A mountain range can be represented as a broken line that starts and ends at sea level (altitude 0) and has strictly positive altitudes in between. These in-between altitudes are called inner altitudes. Alice and Bob start climbing from both ends. They can move along the range, back and forth, but they must stay at the same altitude as each other.
The effort of a path taken by Alice is the sum of absolute differences in altitudes along the path. If Alice's path begins at altitude , changes direction at altitudes , and ends at altitude , then the effort is . Bob makes an equal effort during the same time.
Find the smallest effort Alice and Bob need to make in order to meet.
Input
The mountain range is given as an array of integers, the altitudes of the segment endpoints. The first line contains . The second line contains the integers. The first and last integers are always 0.
Output
Print a single integer, the minimum effort required for Alice and Bob to meet. If they cannot meet, print NO.
Constraints
Inner altitudes are between 1 and 1,000,000.