Special Array Operation
Practice
3.4 (20 votes)
Ad Hoc
Data structures
Easy
Heaps
Trees
Problem
86% Success 7630 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array A of size N. You can perform an operation in which you will remove the largest and the smallest element from the array and add  their difference back into the array. So, the size of the array will decrease by 1 after each oepration. You are given Q tasks and in each task, you are given an integer K. For each task, you have to tell sum of all the elements in the array after K operations.

Input:

First line contains two space-separated integers N and Q, denoting the number of elements in array and number of queries respectively.

Next line contains N space-separated integers denoting elements of the array.

Next Q lines contain a single integer K.

Output:

For each task, print answer in a new line.

Constraints:

2 <= N <= 105

1 <= Q <= 105

0 <= A[i] <= 109

0 <= K < N

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
95 votes
Tags:
ApprovedEasyHeapsOpenPriority Queue
Points:20
114 votes
Tags:
ApprovedEasyOpenPriority queueTrees
Points:20
9 votes
Tags:
Data StructuresPriority queueTrees