#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,N) for(ll i = 0; i < N; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
signed main() {
cin.tie(0); ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> R(N);
vector<int> W(M);
FOR(i,N) cin >> R[i];
FOR(i,M) cin >> W[i];
priority_queue<int, vector<int>, greater<int>> available;
FOR(i,N) available.push(i);
queue<int> wait;
map<int,int> park;
int res = 0;
FOR(i, 2*M) {
int c;
cin >> c;
if (c < 0) {
available.push(park[-(c)]);
} else {
wait.push(c);
while (available.size() && wait.size()) {
park[wait.front()] = available.top();
res += W[wait.front() - 1] * R[available.top()];
wait.pop();
available.pop();
}
}
}
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |