-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
81 lines (75 loc) · 1.91 KB
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define fi first
#define lc (x<<1)
#define se second
#define U unsigned
#define rc (x<<1|1)
#define Re register
#define LL long long
#define MP std::make_pair
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(Re int i = a;i <= b;++i)
#define ROF(i,a,b) for(Re int i = a;i >= b;--i)
#define SFOR(i,a,b,c) for(Re int i = a;i <= b;i+=c)
#define SROF(i,a,b,c) for(Re int i = a;i >= b;i-=c)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
#define int LL
const int MAXN = 200000 + 50;
int N,M,head = 1;
int f[MAXN];
struct Node{
int val,num,id;
bool operator < (const Node &t) const{
return val == t.val ? id < t.id : val < t.val;
}
}a[MAXN];
signed main(){
scanf("%I64d%I64d",&N,&M);
FOR(i,1,N) scanf("%I64d",&a[i].num);
FOR(i,1,N) scanf("%I64d",&a[i].val);
FOR(i,1,N) a[i].id = i;std::sort(a+1,a+N+1);FOR(i,1,N) f[a[i].id] = i;
while(M--){
int x,y;
scanf("%I64d%I64d",&x,&y);
int ans = 0,now = f[x];
if(a[now].num >= y){
ans += a[now].val*y;
a[now].num -= y;
y = 0;
}
else{
ans += a[now].num*a[now].val;
y -= a[now].num;
a[now].num = 0;
}
if(y){
for(;head<=N;){
if(a[head].num >= y){
ans += a[head].val*y;
a[head].num -= y;
y = 0;break;
}else{
ans += a[head].val*a[head].num;
y -= a[head].num;
a[head].num = 0;
++head;
}
}
}
if (head>N||y) puts("0");
else printf("%I64d\n",ans);
}
return 0;
}