#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int n, m;
cin>>n>>m;
int a[n + 5], b[m + 5];
priority_queue<int> q;
for(int i=1; i<=n; i++)
{
cin>>a[i];
q.push(-i);
}
for(int i=1; i<=m; i++) cin>>b[i];
int park[m + 5];
vector<int> proc;
int ans = 0;
for(int i=1; i<=2*m; i++)
{
int x;
cin>>x;
if (x > 0) // join
{
if (q.size() > 0) // there is a parking
{
int cur = q.top();
cur *= -1;
park[x] = cur;
ans += a[cur] * b[x];
q.pop();
}
else // there is no parking
{
proc.push_back(x);
}
}
else
{
q.push(-park[-x]);
bool f = 0;
if (q.size() > 0 && proc.size() > 0)
{
f = 1;
reverse(proc.begin(), proc.end());
}
while(q.size() > 0 && proc.size() > 0)
{
int cur = q.top();
cur *= -1;
int idx = proc.back();
park[idx] = cur;
ans += a[cur] * b[idx];
q.pop();
proc.pop_back();
}
if (f && proc.size() > 0) reverse(proc.begin(), proc.end());
}
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
4 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
708 KB |
Output is correct |