In contests, use the built-in heap: C++ (max-heap by default): priority_queue<int> pq; For min-heap: priority_queue<int, vector<int>, greater<int>> pq; Python (min-heap): heapq.heappush(heap, val) heapq.heappop(heap) For max-heap: negate values or use tuples with negative priority.
Don't implement heaps from scratch in contests. Use the standard library.