Skip to main content

Plank the Trail

Challenge on lesson: Checkpoint — Search

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** A trail has n checkpoints with heights h_1..h_n. Between checkpoint i-1 and i the climbers face an upward step of max(0, h_i - h_{i-1}). You may install wooden planks: one plank reduces the upward step along one gap by 1 (any number per gap). With at most k planks, minimize the **largest remaining upward step**. Print that step. **Input:** Line 1: n k (2 <= n <= 10^5, 0 <= k <= 10^14). Line 2: n heights (0 <= h_i <= 10^9). **Output:** One integer. **Example:** 4 2 / 1 5 5 9 -> up-steps 4, 0, 4; two planks on one gap leave 2 -> 2.

Back to lesson: Checkpoint — Search