#include "bits/stdc++.h"
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> R(N), W(M), NOW(M, -1);
queue<int> Q;
priority_queue<int, vector<int>, greater<int> > PQ;
for (int i = 0; i < N; i++) cin >> R[i];
for (int i = 0; i < M; i++) cin >> W[i];
for (int i = 0; i < N; i++) {
PQ.push(i);
}
int ANS = 0;
for (int i = 0; i < M * 2; i++) {
int X;
cin >> X;
if (X >= 0) {
X--;
if (PQ.empty()) {
Q.push(X);
}
else {
int Y = PQ.top();
PQ.pop();
ANS += R[Y] * W[X];
NOW[X] = Y;
}
}
else {
X = -X;
X--;
if (Q.empty()) {
PQ.push(NOW[X]);
NOW[X] = -1;
}
else {
NOW[Q.front()] = NOW[X];
ANS += R[NOW[X]] * W[Q.front()];
NOW[X] = -1;
Q.pop();
}
}
}
cout << ANS << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |