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;
}
相关推荐
LDR00612 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
Luminous.12 天前
C语言--day30
c语言·开发语言
玖玥拾12 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
謓泽12 天前
C语言不是语法,是通往机器的地图。
c语言·开发语言
不会C语言的男孩12 天前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言
2601_9516438812 天前
C语言长文整理,关键字和数据类型
c语言·数据类型·关键字·嵌入式开发·格式化输出
m0_5474866612 天前
《C#语言程序设计与实践》 全套PPT课件
c语言·c#·c语言程序设计
✎ ﹏梦醒͜ღ҉繁华落℘12 天前
编程基础 --高内聚,低耦合
c语言·单片机
QK_0012 天前
C语言 static 关键字三大作用
c语言·开发语言
隔窗听雨眠12 天前
C语言函数递归从入门到精通(下):性能优化与工程实践
c语言·算法·性能优化