B. Lunar New Year and Food Ordering
Lunar New Year is approaching, and Bob is planning to go for a famous restaurant — “Alice’s”.
The restaurant “Alice’s” serves n kinds of food. The cost for the i-th kind is always ci. Initially, the restaurant has enough ingredients for serving exactly ai dishes of the i-th kind. In the New Year’s Eve, m customers will visit Alice’s one after another and the j-th customer will order dj dishes of the tj-th kind of food. The (i+1)-st customer will only come after the i-th customer is completely served.
Suppose there are ri dishes of the i-th kind remaining (initially ri=ai). When a customer orders 1 dish of the i-th kind, the following principles will be processed.
If ri>0, the customer will be served exactly 1 dish of the i-th kind. The cost for the dish is ci. Meanwhile, ri will be reduced by 1.
Otherwise, the customer will be served 1 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 1.
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 0.
If the customer doesn’t leave after the dj dishes are served, the cost for the customer will be the sum of the cost for these dj dishes.
Please determine the total cost for each of the m customers.
Input
The first line contains two integers n and m (1≤n,m≤105), representing the number of different kinds of food and the number of customers, respectively.
The second line contains n positive integers a1,a2,…,an (1≤ai≤107), where ai denotes the initial remain of the i-th kind of dishes.
The third line contains n positive integers c1,c2,…,cn (1≤ci≤106), where ci denotes the cost of one dish of the i-th kind.
The following m lines describe the orders of the m customers respectively. The j-th line contains two positive integers tj and dj (1≤tj≤n, 1≤dj≤107), representing the kind of food and the number of dishes the j-th customer orders, respectively.
Output
Print m lines. In the j-th line print the cost for the j-th customer.
Examples
Input
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
Output
22
24
14
10
39
Input
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
Output
36
396
3996
39996
399996
0
Input
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
Output
36
11058
99996
4333326
0
0
Note
In the first sample, 5 customers will be served as follows.
Customer 1 will be served 6 dishes of the 2-nd kind, 1 dish of the 4-th kind, and 1 dish of the 6-th kind. The cost is 6⋅3+1⋅2+1⋅2=22. The remain of the 8 kinds of food will be {8,0,2,0,4,4,7,5}.
Customer 2 will be served 4 dishes of the 1-st kind. The cost is 4⋅6=24. The remain will be {4,0,2,0,4,4,7,5}.
Customer 3 will be served 4 dishes of the 6-th kind, 3 dishes of the 8-th kind. The cost is 4⋅2+3⋅2=14. The remain will be {4,0,2,0,4,0,7,2}.
Customer 4 will be served 2 dishes of the 3-rd kind, 2 dishes of the 8-th kind. The cost is 2⋅3+2⋅2=10. The remain will be {4,0,0,0,4,0,7,0}.
Customer 5 will be served 7 dishes of the 7-th kind, 3 dishes of the 1-st kind. The cost is 7⋅3+3⋅6=39. The remain will be {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 6 dishes of the second kind, so the cost is 66⋅6=396.
In the third sample, some customers may not be served what they order. For example, the second customer is served 6 dishes of the second kind, 6 of the third and 1 of the fourth, so the cost is 66⋅6+666⋅6+6666⋅1=11058.
思路
题目大意:客人来点餐,优先给他要的那款菜,如果不够或没有则按便宜从前往后给,如果价格一样则按下标从前往后给。如果客人点的菜比现有的菜多,则不进行计算。
模拟,直接根据题目要求进行模拟,开两个数组分别录入数量和价钱的同时,用一个结构体装编号和价格,进行排序,根据价格和下标,从小到大排。每次录入 t 和 d 进行模拟。用一个mark记录便宜菜式算到哪,不需要重新跑循环。我一开始用int,WA了,后来直接全部开long long就过了。
代码
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 100000+5
typedef long long ll;
ll a[100005],c[100005]; //一定要开long long 我就因为这个WA了好多次!
struct node {
ll num;
ll money;
}dish[maxn];
bool cmp(node a,node b) {
if(a.money == b.money) {
return a.num < b.num;
}
else {
return a.money < b.money;
}
}
int main()
{
ll n,m;
cin >> n >> m;
ll sum = 0;
for(int i=1;i<=n;i++) {
cin >> a[i];
sum += a[i];
}
for(ll i=1;i<=n;i++) {
cin >> c[i];
dish[i].num = i;
dish[i].money = c[i];
}
sort(dish+1,dish+n+1,cmp);
int mark = 1;
while(m--)
{
ll total = 0;
ll t,d;
cin >> t >> d;
if(sum >= d) {
sum -= d;
}
else {
puts("0");
sum = 0;
continue;
}
if(a[t] >= d) {
total = d * c[t];
a[t] -= d;
}
else {
d -= a[t];
total += a[t] * c[t];
a[t] = 0;
while(d)
{
while(!a[dish[mark].num]) {
mark ++;
}
ll temp = dish[mark].num;
if(a[temp] >= d) {
a[temp] -= d;
total += d * c[temp];
d = 0;
}
else {
total += a[temp] * c[temp];
d -= a[temp];
a[temp] = 0;
}
}
}
cout << total << endl;
}
return 0;
}