You are given N elements to be inserted into a priority queue. Each element is represented as a pair (x, y), where:
A higher value of y indicates a higher priority.
If two elements have the same priority, the element inserted earlier is removed first (FIFO order for equal priorities).
Your task is to find the sum of the first K elements removed from the priority queue.
The first line contains an integer N, the number of elements.
The next N lines each contain two integers:
The last line contains an integer K, the number of elements to remove from the priority queue.
Print a single integer representing the sum of the values of the first K elements removed from the priority queue.
4
3 1
7 3
4 2
5 3
2
12
The elements are removed in decreasing order of priority.
Insertion order:
(3,1)
(7,3)
(4,2)
(5,3)
Removal order:
(7,3)
(5,3) // Same priority as 7, but inserted later, so removed after 7
(4,2)
(3,1)
The first K = 2 removed elements have values 7 and 5.
Sum = 7 + 5 = 12.
Intuit • Pending
Intuit • Pending
Intuit • Pending
Intuit • Pending