Submission #366900

#TimeUsernameProblemLanguageResultExecution timeMemory
366900kostia244Garage (IOI09_garage)C++17
100 / 100
1 ms492 KiB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
const int maxn = 2e3 + 5, C = 1;
using ll = long long;
int n, m, b[maxn], r[maxn], w[maxn], dead[maxn], crap[maxn];
ll ans = 0;
queue<int> q;
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++) cin >> r[i];
	for(int i = 1; i <= m; i++) cin >> w[i];
	for(int t, i = 0; i < 2*m; i++) {
		cin >> t;
		if(t > 0) {
			q.push(t);
		} else {t*=-1;
			dead[t] = 1;
			if(crap[t]) b[crap[t]] = 0;
		}
		while(!q.empty() && dead[q.front()]) q.pop();
		if(!q.empty()) {
			int fp = 1;
			while(b[fp]) fp++;
			if(fp <= n) {
				b[fp] = 1;
				crap[q.front()] = fp;
				ans += r[fp]*w[q.front()];
				q.pop();
			} 
		}
	}
	cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...