#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
int n, m;
cin >> n >> m;
vector<int> v(n+5, 0);
int a[1000];
for(int i = 0; i < n; i++) cin >> a[i];
map<int,int> M;
for(int i = 0; i < m; i++) cin >> M[i+1];
queue<int> Q;
int cnt = 0, ans = 0;
for(int i = 0; i < 2*m; i++)
{
int x; cin >> x;
if(x>0)
{
if(cnt==n) Q.push(x);
else{
cnt++;
for(int j = 0; j < n; j++)
{
if(v[j]) continue;
v[j]=x, ans+=a[j]*M[x];
break;
}
}
}
else{
x*=-1;
for(int j = 0; j < n; j++)
{
if(v[j]!=x) continue;
v[j]=0, cnt--;
if(!Q.empty()){
int z = Q.front(); Q.pop();
v[j]=z, ans+=a[j]*M[z], cnt++;
}
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
3 ms |
492 KB |
Output is correct |
19 |
Correct |
3 ms |
492 KB |
Output is correct |
20 |
Correct |
4 ms |
492 KB |
Output is correct |