#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
int main(){
//freopen("lemonade.in", "r", stdin);
//freopen("lemonade.out", "w", stdout);
int n,m;
cin >> n >> m;
int rates[100];
for(int i=0; i<n; i++){
cin >> rates[i];
}
int weights[2000];
for(int i=0; i<m; i++){
cin >> weights[i];
}
int cars[4000];
for(int i=0; i<2*m; i++){
cin >> cars[i];
}
int spaces[100];
for(int i=0; i<n; i++){
spaces[i]=-1;
}
ll ans=0;
int idx=0;
int waiting[2000];
for(int i=0; i<2000; i++) waiting[i]=-1;
for(int i=0; i<2*m; i++){
int caridx=abs(cars[i])-1;
if(cars[i]>0){
if(waiting[0]!=-1){
bool nqma=false;
for(int j=0; j<n; j++){
if(spaces[j]==-1){
break;
}
if(j==n-1) nqma=true;
}
if(nqma){
waiting[idx]=caridx;
idx++;
}
else{
for(int j=0; j<n; j++){
if(spaces[j]==-1){
spaces[j]=waiting[0];
ans+=rates[j]*weights[waiting[0]];
break;
}
}
//0 1 2 3 4 -1
//1 2 3 4 -1 -1
for(int j=0; j<idx-1; j++){
waiting[j]=waiting[j+1];
}
idx--;
waiting[idx]=caridx;
idx++;
}
}
else{
bool nqma=false;
for(int j=0; j<n; j++){
if(spaces[j]==-1){
spaces[j]=caridx;
ans+=rates[j]*weights[caridx];
break;
}
if(j==n-1) nqma=true;
}
if(nqma){
waiting[idx]=caridx;
idx++;
}
}
}
else if(cars[i]<0){
int bruh;
for(int j=0; j<n; j++){
if(spaces[j]==caridx){
spaces[j]=-1;
bruh=j;
break;
}
}
if(waiting[0]!=-1){
spaces[bruh]=waiting[0];
ans+=rates[bruh]*weights[waiting[0]];
for(int j=0; j<idx-1; j++){
waiting[j]=waiting[j+1];
}
idx--;
waiting[idx]=-1;
}
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |