CF1106B.Lunar New Year and Food Ordering

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Lunar New Year is approaching, and Bob is planning to go for a famous restaurant — "Alice's".

The restaurant "Alice's" serves nn kinds of food. The cost for the ii-th kind is always cic_i. Initially, the restaurant has enough ingredients for serving exactly aia_i dishes of the ii-th kind. In the New Year's Eve, mm customers will visit Alice's one after another and the jj-th customer will order djd_j dishes of the tjt_j-th kind of food. The (i+1)(i + 1)-st customer will only come after the ii-th customer is completely served.

Suppose there are rir_i dishes of the ii-th kind remaining (initially ri=air_i = a_i). When a customer orders 11 dish of the ii-th kind, the following principles will be processed.

  1. If ri>0r_i \gt 0, the customer will be served exactly 11 dish of the ii-th kind. The cost for the dish is cic_i. Meanwhile, rir_i will be reduced by 11.
  2. Otherwise, the customer will be served 11 dish of the cheapest available kind of food if there are any. If there are multiple cheapest kinds of food, the one with the smallest index among the cheapest will be served. The cost will be the cost for the dish served and the remain for the corresponding dish will be reduced by 11.
  3. If there are no more dishes at all, the customer will leave angrily. Therefore, no matter how many dishes are served previously, the cost for the customer is 00.

If the customer doesn't leave after the djd_j dishes are served, the cost for the customer will be the sum of the cost for these djd_j dishes.

Please determine the total cost for each of the mm customers.

农历新年即将来临,鲍勃计划前往一家著名的餐厅——“爱丽丝餐厅”。

“爱丽丝餐厅”提供 nn 种食物。第 ii 种食物的单价恒为 cic_i。初始时,餐厅拥有恰好足够制作 aia_i 份第 ii 种食物的食材。在除夕夜,将有 mm 位顾客依次光临爱丽丝餐厅,其中第 jj 位顾客将点 djd_j 份第 tjt_j 种食物。第 (i+1)(i + 1) 位顾客仅在第 ii 位顾客被完全服务完毕后才到来。

假设当前第 ii 种食物还剩余 rir_i 份(初始时 ri=air_i = a_i)。当一位顾客点 1 份第 ii 种食物时,将按以下规则处理:

  1. 若 ri>0r_i \gt 0,则该顾客将被恰好供应 1 份第 ii 种食物,费用为 cic_i;同时 rir_i 减少 1。
  2. 否则,若尚有其他种类的食物仍有库存,则该顾客将被供应 1 份当前最便宜的、仍有库存的食物;若存在多个价格相同的最便宜种类,则选择其中下标最小的那种。所供应食物的费用即为该种食物的单价,且对应种类的剩余份数 rkr_k 减少 1。
  3. 若所有种类的食物均已售罄,则该顾客将愤怒离开。因此,无论此前已供应多少份食物,该顾客的总费用为 00。

若该顾客在成功获得全部 djd_j 份食物后未离开,则其费用即为这 djd_j 份食物费用的总和。

请计算每位顾客(共 mm 位)的总费用。

输入格式

The first line contains two integers nn and mm (1≤n,m≤1051 \leq n, m \leq 10^5), representing the number of different kinds of food and the number of customers, respectively.

The second line contains nn positive integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1071 \leq a_i \leq 10^7), where aia_i denotes the initial remain of the ii-th kind of dishes.

The third line contains nn positive integers c1,c2,…,cnc_1, c_2, \ldots, c_n (1≤ci≤1061 \leq c_i \leq 10^6), where cic_i denotes the cost of one dish of the ii-th kind.

The following mm lines describe the orders of the mm customers respectively. The jj-th line contains two positive integers tjt_j and djd_j (1≤tj≤n1 \leq t_j \leq n, 1≤dj≤1071 \leq d_j \leq 10^7), representing the kind of food and the number of dishes the jj-th customer orders, respectively.

第一行包含两个整数 nn 和 mm(1≤n,m≤1051 \leq n, m \leq 10^5),分别表示食物种类数和顾客数。

第二行包含 nn 个正整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1071 \leq a_i \leq 10^7),其中 aia_i 表示第 ii 种食物的初始剩余数量。

第三行包含 nn 个正整数 c1,c2,…,cnc_1, c_2, \ldots, c_n(1≤ci≤1061 \leq c_i \leq 10^6),其中 cic_i 表示第 ii 种食物每份的价格。

接下来的 mm 行依次描述 mm 位顾客的订单。第 jj 行包含两个正整数 tjt_j 和 djd_j(1≤tj≤n1 \leq t_j \leq n,1≤dj≤1071 \leq d_j \leq 10^7),分别表示第 jj 位顾客所点食物的种类编号及其订购份数。

输出格式

Print mm lines. In the jj-th line print the cost for the jj-th customer.

输出 mm 行。在第 jj 行输出第 jj 位顾客的花费。

输入输出样例

  • 输入#1

    8 5
    8 6 2 1 4 5 7 5
    6 3 3 2 6 2 3 2
    2 8
    1 4
    4 7
    3 4
    6 10

    输出#1

    22
    24
    14
    10
    39
  • 输入#2

    6 6
    6 6 6 6 6 6
    6 66 666 6666 66666 666666
    1 6
    2 6
    3 6
    4 6
    5 6
    6 66

    输出#2

    36
    396
    3996
    39996
    399996
    0
  • 输入#3

    6 6
    6 6 6 6 6 6
    6 66 666 6666 66666 666666
    1 6
    2 13
    3 6
    4 11
    5 6
    6 6

    输出#3

    36
    11058
    99996
    4333326
    0
    0

说明/提示

In the first sample, 55 customers will be served as follows.

  1. Customer 11 will be served 66 dishes of the 22-nd kind, 11 dish of the 44-th kind, and 11 dish of the 66-th kind. The cost is 6⋅3+1⋅2+1⋅2=226 \cdot 3 + 1 \cdot 2 + 1 \cdot 2 = 22. The remain of the 88 kinds of food will be 8,0,2,0,4,4,7,5{8, 0, 2, 0, 4, 4, 7, 5}.
  2. Customer 22 will be served 44 dishes of the 11-st kind. The cost is 4⋅6=244 \cdot 6 = 24. The remain will be 4,0,2,0,4,4,7,5{4, 0, 2, 0, 4, 4, 7, 5}.
  3. Customer 33 will be served 44 dishes of the 66-th kind, 33 dishes of the 88-th kind. The cost is 4⋅2+3⋅2=144 \cdot 2 + 3 \cdot 2 = 14. The remain will be 4,0,2,0,4,0,7,2{4, 0, 2, 0, 4, 0, 7, 2}.
  4. Customer 44 will be served 22 dishes of the 33-rd kind, 22 dishes of the 88-th kind. The cost is 2⋅3+2⋅2=102 \cdot 3 + 2 \cdot 2 = 10. The remain will be 4,0,0,0,4,0,7,0{4, 0, 0, 0, 4, 0, 7, 0}.
  5. Customer 55 will be served 77 dishes of the 77-th kind, 33 dishes of the 11-st kind. The cost is 7⋅3+3⋅6=397 \cdot 3 + 3 \cdot 6 = 39. The remain will be 1,0,0,0,4,0,0,0{1, 0, 0, 0, 4, 0, 0, 0}.

In the second sample, each customer is served what they order except the last one, who leaves angrily without paying. For example, the second customer is served 66 dishes of the second kind, so the cost is 66⋅6=39666 \cdot 6 = 396.

In the third sample, some customers may not be served what they order. For example, the second customer is served 66 dishes of the second kind, 66 of the third and 11 of the fourth, so the cost is 66⋅6+666⋅6+6666⋅1=1105866 \cdot 6 + 666 \cdot 6 + 6666 \cdot 1 = 11058.

在第一个样例中,将为 55 位顾客提供服务,具体如下:

  1. 顾客 11 将获得 66 份第 22 种食物、11 份第 44 种食物和 11 份第 66 种食物。费用为 6⋅3+1⋅2+1⋅2=226 \cdot 3 + 1 \cdot 2 + 1 \cdot 2 = 22。剩余的 88 种食物数量为 8,0,2,0,4,4,7,5{8, 0, 2, 0, 4, 4, 7, 5}。
  2. 顾客 22 将获得 44 份第 11 种食物。费用为 4⋅6=244 \cdot 6 = 24。剩余食物数量为 4,0,2,0,4,4,7,5{4, 0, 2, 0, 4, 4, 7, 5}。
  3. 顾客 33 将获得 44 份第 66 种食物和 33 份第 88 种食物。费用为 4⋅2+3⋅2=144 \cdot 2 + 3 \cdot 2 = 14。剩余食物数量为 4,0,2,0,4,0,7,2{4, 0, 2, 0, 4, 0, 7, 2}。
  4. 顾客 44 将获得 22 份第 33 种食物和 22 份第 88 种食物。费用为 2⋅3+2⋅2=102 \cdot 3 + 2 \cdot 2 = 10。剩余食物数量为 4,0,0,0,4,0,7,0{4, 0, 0, 0, 4, 0, 7, 0}。
  5. 顾客 55 将获得 77 份第 77 种食物和 33 份第 11 种食物。费用为 7⋅3+3⋅6=397 \cdot 3 + 3 \cdot 6 = 39。剩余食物数量为 1,0,0,0,4,0,0,0{1, 0, 0, 0, 4, 0, 0, 0}。

在第二个样例中,除最后一位顾客外,其余每位顾客均按其订单提供食物;而最后一位顾客因不满而愤怒离店,未支付任何费用。例如,第二位顾客获得 66 份第 22 种食物,因此费用为 66⋅6=39666 \cdot 6 = 396。

在第三个样例中,部分顾客可能无法完全获得其所点的食物。例如,第二位顾客获得 66 份第 22 种食物、66 份第 33 种食物和 11 份第 44 种食物,因此费用为 66⋅6+666⋅6+6666⋅1=1105866 \cdot 6 + 666 \cdot 6 + 6666 \cdot 1 = 11058。

输入解题思路,AI测评打分。不知道怎么写?

首页