#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,l,r) for(int i=(l); i<(r); i++)
#define all(x) x.begin(),x.end()
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N,M;
cin>>N>>M;
vector<ll> R(N);
rep(i,N){
cin>>R[i];
}
vector<ll> W(M);
rep(i,M){
cin>>W[i];
}
vector<ll> use(N,-1);
queue<ll> wait;
ll ans=0;
rep(i,2*M){
ll t;
cin>>t;
if(t>0){
t--;
rep(j,N){
if(use[j]==-1){
use[j]=t;
ans+=W[t]*R[j];
t=-1;
break;
}
}
if(t!=-1){
wait.push(t);
}
}
else{
t=-1-t;
rep(j,N){
if(use[j]==t){
use[j]=-1;
if(!wait.empty()){
use[j]=wait.front();
ans+=W[wait.front()]*R[j];
wait.pop();
}
break;
}
}
}
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |