Practice · 4 of 5
Two-Heap Gifts
CHALLENGE
Difficulty: intermediate+25 XP
**Description:** You receive n gifts in order, gift i worth w_i. After each receipt,
**while you hold more than one gift**, immediately give away the smallest-valued
gift you hold. After all n steps, print the total worth of the gifts you still hold.
**Input:** Line 1: n (1 <= n <= 2*10^5). Line 2: n integers w_i (|w_i| <= 10^9).
**Output:** One integer — the total worth of kept gifts.
**Example:** 4 / 3 1 4 2 -> 4 (+3 hold {3}; +1 give 1, hold {3}; +4 give 3, hold {4}; +2 give 2, hold {4}).
Press Submit to check your solution.
Back to lesson: Practice: STL Problem Set