#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 2010;
int occupied_by[N];
int rate[N];
int weight[N];
queue<int> que;
pii events[2*N];
int n, m;
int first_free()
{
Loop (i,0,n)
if (occupied_by[i] == -1)
return i;
return -1;
}
void leave(int x)
{
Loop (i,0,n)
if (occupied_by[i] == x)
occupied_by[i] = -1;
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n >> m;
Loop (i,0,n)
cin >> rate[i];
Loop (i,0,m)
cin >> weight[i];
Loop (i,0,2*m) {
int x;
cin >> x;
if (x>0)
--x;
events[i] = {i, x};
}
sort(events, events+2*m);
memset(occupied_by, -1, sizeof(occupied_by));
int ans = 0;
Loop (i,0,2*m) {
int x = events[i].second;
if (x >= 0)
que.push(x);
else
leave(~x);
while (que.size() && first_free() != -1) {
int p = first_free();
int c = que.front();
que.pop();
occupied_by[p] = c;
ans += rate[p] * weight[c];
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |