#include<bits/stdc++.h>
using namespace std;
vector<int> weight,cars_to_lot,vr;
priority_queue<int, vector<int>, greater<int>> pq;
int n,m;
queue<int> wait_queue;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
vr.resize(n);
for(auto &r:vr) cin>>r;
for(int i=0;i<n;i++) pq.push(i);
weight.resize(m);
for(auto &w:weight) cin>>w;
cars_to_lot.resize(m, -1);
int money=0;
for(int i=0;i<2*m;i++) {
int x;cin>>x;
if (x>0) {
if (pq.empty()) wait_queue.push(x);
else {
int lot=pq.top();
pq.pop();
cars_to_lot[x-1]=lot;
money+=vr[lot]*weight[x-1];
}
} else {
x*=-1;
pq.push(cars_to_lot[x-1]);
if (!wait_queue.empty()) {
pq.pop();
int lot_to_use = cars_to_lot[x-1];
int x = wait_queue.front();
cars_to_lot[x-1]=lot_to_use;
money+=vr[lot_to_use]*weight[x-1];
wait_queue.pop();
}
cars_to_lot[x-1]=-1;
}
}
cout<<money;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |