제출 #535806

#제출 시각아이디문제언어결과실행 시간메모리
535806devariaotaGarage (IOI09_garage)C++17
100 / 100
1 ms452 KiB
#include <bits/stdc++.h>
using namespace std;

#define nyahalo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define otsumiko exit(0);
#define mikodanye priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > >
#define mikochi priority_queue<long long, vector<long long>, greater<long long> >

int main() {
  nyahalo
  long long n, m, x, ans = 0;
  cin >> n >> m;
  long long r[n+1], w[m+1], a[m+1], b[n+1];
  queue<long long> qu;
  for (long long i=1; i<=n; i++) {
    cin >> r[i];
    b[i] = -1;
  }
  for (long long i=1; i<=m; i++) {
    cin >> w[i];
    a[i] = -1;
  }
  for (long long i=1; i<=2*m; i++) {
    cin >> x;
    if (x>0) {
      if (!qu.empty()) {
        qu.push(x);
      } else {
        for (long long j=1; j<=n; j++) {
          if (b[j] == -1) {
            b[j] = x;
            a[x] = j;
            ans += w[x]*r[j];
            break;
          }
        }
        if (a[x] == -1) {
          qu.push(x);
        }
      }
    } else {
      x = abs(x);
      b[a[x]] = -1;
      if (!qu.empty()) {
        ans += w[qu.front()]*r[a[x]];
        b[a[x]] = qu.front();
        a[qu.front()] = a[x];
        qu.pop();
      }
      a[x] = -1;
    }
  }
  cout << ans << "\n";
  otsumiko
}
#Verdict Execution timeMemoryGrader output
Fetching results...