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;
}
相关推荐
炸膛坦客8 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
程序员Rock15 小时前
上位机开发-MODBUS面试常见问题
c语言·c++·面试·职场和发展·上位机
blevoice20 小时前
抖抖燃脂机单 AC6966B 主控调试踩坑:Type-C 整机供电喇叭无声排障
c语言·开发语言·单片机
小小晓.20 小时前
C++小白记:C风格字符串和数组用法
c语言·开发语言·c++
深念Y21 小时前
开发者如何清理和迁移C盘堆积的垃圾
c语言·开发语言
十月的皮皮1 天前
STM32从零到量产开发:四路继电器工业控制模块开发实战笔记(小白视角)
c语言·笔记·stm32·stm32cubemx·hal库
不正经学生1 天前
C 语言函数深入剖析(基础篇)—— 从零理解函数的每一个细节
c语言·开发语言·数据结构·算法·c#
天空'之城1 天前
C 语言工业级通用组件手写 10:字节序转换
c语言·字节序·大小端·嵌入式通信·工业级组件
C++ 老炮儿的技术栈1 天前
OpenCV 实现社保卡透视矫正:Hough 直线提取四角并转正证件图像
c语言·c++·人工智能·opencv·算法·计算机视觉·c
我叫洋洋1 天前
C ++ [ hello world ]
c语言·c++·算法