CF1697B.Promo

传统题 时间 2000 ms 内存 256 MiB 4 尝试 1 已通过 1 标签

Promo

The store sells nn items, the price of the ii-th item is pip_i. The store's management is going to hold a promotion: if a customer purchases at least xx items, yy cheapest of them are free.

The management has not yet decided on the exact values of xx and yy. Therefore, they ask you to process qq queries: for the given values of xx and yy, determine the maximum total value of items received for free, if a customer makes one purchase.

Note that all queries are independent; they don't affect the store's stock.

Input

The first line contains two integers nn and qq (1n,q21051 \le n, q \le 2 \cdot 10^5) — the number of items in the store and the number of queries, respectively.

The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n (1pi1061 \le p_i \le 10^6), where pip_i — the price of the ii-th item.

The following qq lines contain two integers xix_i and yiy_i each (1yixin1 \le y_i \le x_i \le n) — the values of the parameters xx and yy in the ii-th query.

Output

For each query, print a single integer — the maximum total value of items received for free for one purchase.

Note

In the first query, a customer can buy three items worth 5,3,55, 3, 5, the two cheapest of them are 3+5=83 + 5 = 8.

In the second query, a customer can buy two items worth 55 and 55, the cheapest of them is 55.

In the third query, a customer has to buy all the items to receive the three cheapest of them for free; their total price is 1+2+3=61 + 2 + 3 = 6.

Samples

5 3
5 3 1 5 2
3 2
1 1
5 3
8
5
6

在线编程 IDE

建议全屏模式获得最佳体验