#include <bits/stdc++.h>
using namespace std;
void testCase() {
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<int> w(m + 1);
for (int i = 1; i <= m; ++i) {
cin >> w[i];
}
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 1; i <= n; ++i) {
pq.emplace(i);
}
queue<int> q;
vector<int> slot(m + 1);
vector<bool> del(m + 1);
int ans = 0;
for (int i = 1; i <= 2 * m; ++i) {
int x;
cin >> x;
if (x > 0) {
if (pq.empty()) {
q.emplace(x);
} else {
slot[x] = pq.top();
pq.pop();
ans += w[x] * a[slot[x]];
}
} else {
x = -x;
if (slot[x] == 0) {
del[x] = true;
} else {
pq.emplace(slot[x]);
while (!q.empty() && !pq.empty()) {
while (!q.empty() && del[q.front()]) {
q.pop();
}
if (!q.empty()) {
slot[q.front()] = pq.top();
pq.pop();
ans += w[q.front()] * a[slot[q.front()]];
q.pop();
}
}
}
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |