B. Promo

time limit per test

2 seconds

memory limit per test

256 megabytes

The store sells n items, the price of the i-th item is pi. The store's management is going to hold a promotion: if a customer purchases at least x items, y cheapest of them are free.

The management has not yet decided on the exact values of x and y. Therefore, they ask you to process q queries: for the given values of x and y, 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 n and q (1≤n,q≤2⋅105) --- the number of items in the store and the number of queries, respectively.

The second line contains n integers p1,p2,...,pn (1≤pi≤106), where pi --- the price of the i-th item.

The following q lines contain two integers xi and yi each (1≤yi≤xi≤n) --- the values of the parameters x and y in the i-th query.

Output

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

Example

Input

Copy

复制代码
5 3
5 3 1 5 2
3 2
1 1
5 3

Output

Copy

复制代码
8
5
6

Note

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

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

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=6.

解题说明:此题是一道模拟题,采用贪心算法。首先根据物品价格进行排序,然后先计算出包含前面所有物品的总价。最后根据输入的查询条件直接输出结果即可。

cpp 复制代码
#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long

int main()
{
	ll n, q, x, y, i;
	cin >> n >> q;
	ll p[n + 1]; p[0] = 0;
	for (i = 0; i < n; i++)
	{
		cin >> p[i + 1];
	}
	sort(p, p + n + 1);
	for (i = 1; i <= n; i++)
	{
		p[i] += p[i - 1];
	}
	while (q--) 
	{
		cin >> x >> y;
		cout << (p[n + y - x] - p[n - x]) << endl;
	}
	return 0;
}
相关推荐
inrichtt1 小时前
第八课时:SPI总线揭秘:四线全双工通信的奥
c语言·数据结构·stm32·单片机·嵌入式硬件
wuminyu4 小时前
JMM学习总结:深入理解happens-before关系
java·linux·c语言·jvm·c++
Zwawa6 小时前
让电机走到指定位置:编码器位置环 PID
c语言·单片机·嵌入式
fensnote7 小时前
西门子 S7 PLC 内部寄存器读写 Demo(C 语言)
c语言·开发语言
Logic10110 小时前
C语言/数据结构位运算题解:异或XOR找出被挑选一次的果子编号——只出现一次的数字
c语言·数据结构·算法·数组·位运算·时间复杂度·空间复杂度
dear_bi_MyOnly10 小时前
栈和队列:从原理到应用全解析
c语言·开发语言·c++·学习·分类
从小白到软工11 小时前
4-3 项目-学生成绩管理系统(上)
c语言·开发语言·数据结构·经验分享·ai编程
Code_Solitude12 小时前
c语言:指针(3)+冒泡排序
java·c语言·算法
巴巴媛66613 小时前
【Debug1】程序死机,while(1)循环,怎么办?
c语言·笔记·stm32·嵌入式硬件·bug
Lxx_Zw13 小时前
PID 用顺了,然后呢:增强手术与前馈
c语言·stm32·单片机·嵌入式硬件