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;
}
相关推荐
handler016 分钟前
进程状态流转的本质:Linux 内核队列与底层数据结构解密
linux·运维·c语言·数据结构·c++·笔记·学习
忡黑梨1 小时前
eNSP_DHCP配置
c语言·网络·c++·python·算法·网络安全·智能路由器
她说彩礼65万2 小时前
C语言 动态内存管理
c语言·开发语言·算法
Z文的博客2 小时前
【避坑实录】Qt 4.8.6 + Paho MQTT C客户端 + OpenSSL静态链接的血泪史
c语言·开发语言·qt·嵌入式linux
一行代码一行诗++2 小时前
转义字符和语句
c语言·开发语言·算法
算法鑫探2 小时前
算法与数据结构 以及算法复杂度
c语言·数据结构·算法·新人首发
HABuo3 小时前
【linux(四)】套接字编程--socket套接字及其接口认识
linux·运维·服务器·c语言·c++·ubuntu·centos
流年如夢3 小时前
顺序表 -->增、删、查、改等详细操作
c语言·数据结构
我不是懒洋洋3 小时前
手写一个布隆过滤器:从原理到工业级实现
c语言
小年糕是糕手3 小时前
【C/C++刷题集】栈、stack、队列、queue核心精讲
c语言·开发语言·数据结构·数据库·c++·算法·蓝桥杯