#include <bits/stdc++.h>
/// #pragma GCC optimize ("Ofast")
/// #pragma GCC target ("avx2")
/// #pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<int>;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define int ll
const int MOD = 1000 * 1000 * 1000 + 7;
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N, M; cin >> N >> M; int R[N], W[M]; bool leave[M];
queue<int> Q; int A[N], B[M]; int res = 0;
for(int l = 0; l < N; l++)
cin >> R[l], A[l] = -1;
for(int l = 0; l < M; l++)
cin >> W[l], B[l] = -1, leave[l] = 0;
for(int _ = 0; _ < 2 * M; _++) {
int U; cin >> U;
if(U > 0) { U--; Q.push(U); }
else {
U++; U = -U; leave[U] = 1;
if(B[U] == -1) continue;
A[B[U]] = -1; B[U] = -1;
}
while(!Q.empty()) {
int V = Q.front(); bool ok = 0;
if(leave[V]) { Q.pop(); continue; }
for(int l = 0; l < N; l++) {
if(A[l] != -1) continue;
A[l] = V, B[V] = l;
ok = 1; res += R[l] * W[V];
break;
}
if(ok) Q.pop();
else break;
}
}
cout << res << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
388 KB |
Output is correct |