#P9029. [COCI 2022/2023 #1] Čokolade

[COCI 2022/2023 #1] Čokolade

Background

Lana and Fran are visiting a chocolate factory, and now they want to buy some chocolates.

Problem Description

There are nn different chocolates in the factory, and the price of the ii-th chocolate is cic_i. Lana and Fran want to buy mm chocolates.

Fran has a payment plan:

  • If the chocolate price is less than kk, the full cost of this chocolate will be paid by Lana.
  • Otherwise, Lana will pay kk, and Fran will pay the remaining part, i.e. cikc_i-k.

Lana is not happy with Fran's plan and wants to get back at Fran. Let ll be the amount Lana needs to pay, and ff be the amount Fran needs to pay. Lana will choose a buying plan that minimizes the value of lfl-f.

Since Fran is still hesitating and does not know how many chocolates to buy, Lana wants to know, for the given qq different plans kik_i and mim_i, the minimum value of lfl-f for each plan.

Input Format

The first line contains two integers nn and qq, representing the number of chocolates and the number of queries.

The second line contains nn integers cic_i, in order, representing the price of each chocolate.

The next qq lines each contain two integers kik_i and mim_i, representing Fran's payment threshold and the total number of chocolates to buy.

Output Format

Output qq lines, each containing one integer representing the answer to Lana's query.

5 2
1 9 22 10 19
18 4
5 2
34
-21
7 4
1 5 4 3 7 11 9
5 4
5 7
7 3
4 5
4
16
7
1
3 3
5 6 7
10 1
5 3
3 3
5
12
0

Hint

Subtask Score Special Properties
11 1515 n,q1000,ci,ki106n,q \leq 1000,c_i,k_i\leq 10^6
22 2020 All queries have the same kk
33 3535 No special properties

For 100%100\% of the data, 1min,q105,1ci,ki1091\leq m_i\leq n,q\leq 10^5,1\leq c_i,k_i \leq 10^9.

The full score of this problem is 7070 points.

Translated by ChatGPT 5