#include<bits/stdc++.h>
using namespace std;
int main(){
int N,M;
cin >> N >> M;
vector<long long int> R(N);
for(int i=0;i<N;i++) cin >> R[i];
vector<long long int> W(M);
for(int i=0;i<M;i++) cin >> W[i];
priority_queue<int,vector<int>,greater<int>> que1;
for(int i=0;i<N;i++) que1.push(i);
queue<int> que2;
vector<int> P(M);
long long int ans = 0;
for(int i=0;i<2*M;i++){
int C;
cin >> C;
if(C > 0){
C--;
if(que1.size() == 0) que2.push(C);
else{
int a = que1.top();
que1.pop();
ans += R[a]*W[C];
P[C] = a;
}
}
else{
C++;
C *= -1;
int a = P[C];
que1.push(a);
if(que2.size() > 0){
int b = que1.top();
int c = que2.front();
ans += R[b]*W[c];
P[c] = b;
que1.pop();
que2.pop();
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |