Parking Lot

Interview

Time limit1sMemory limit128 MB

Summary
Simulate cars arriving and leaving a parking lot, assigning each to the lowest-numbered free space or a waiting queue, and sum weight times rate.
Level

Medium4 of 10

Topics
Simulation, Queue, Heap, Implementation
Solved
No attempts yet

Problem

A downtown parking lot has NN parking spaces numbered from 11 to NN. Every morning the lot opens with all spaces empty, and throughout the day it operates by the following rules.

When a car arrives, the attendant checks whether any space is empty. If none is empty, the car waits at the entrance until a space frees up. As soon as a space becomes available (or one is already free on arrival), the car parks in it. If several spaces are free, the car parks in the one with the smallest number. When several cars arrive at once, they line up at the entrance in arrival order; the waiting line behaves like a queue, so the earliest-arriving car parks first.

The parking fee is proportional to the car's weight, not to the time parked. The fee equals the car's weight multiplied by the per-unit-weight rate of the space where it parks.

The attendant knows that MM cars will use the lot today, as well as the exact order in which the cars enter and leave.

Given the per-space rates, the weight of each car, and the enter/leave order, write a program that computes the total revenue the lot earns over the day.

Input

  • The first line contains two integers NN and MM separated by a space.
  • Each of the next NN lines contains the per-unit-weight rate of a space. The ss-th of these lines holds RsR_s, the per-unit-weight rate of space ss.
  • Each of the next MM lines contains the weight of a car. Cars are numbered 11 through MM, independent of the enter/leave order. The kk-th of these lines holds WkW_k, the weight of car kk.
  • Each of the next 2M2M lines contains one integer describing the enter/leave order. A positive integer ii means car ii enters the lot; a negative integer −i-i means car ii leaves the lot.

A car never leaves without having entered. Every car from 11 to MM enters exactly once and leaves exactly once. A car waiting at the entrance never leaves without parking.

  • 1≤N≤1001 \le N \le 100 (number of spaces)
  • 1≤M≤2,0001 \le M \le 2{,}000 (number of cars)
  • 1≤Rs≤1001 \le R_s \le 100 (per-unit-weight rate of space ss)
  • 1≤Wk≤10,0001 \le W_k \le 10{,}000 (weight of car kk)

Output

Print a single integer on one line: the total revenue the lot earns over the day.

Hint

For example, suppose the space rates are 2,3,52, 3, 5; the car weights are 200,100,300,800200, 100, 300, 800; and the enter/leave order is 3,2,−3,1,4,−4,−2,−13, 2, -3, 1, 4, -4, -2, -1. Then:

  • Car 33 parks in space 11. Its fee is 300×2=600300 \times 2 = 600.
  • Car 22 parks in space 22. Its fee is 100×3=300100 \times 3 = 300.
  • Car 11 parks in space 11, freed by car 33. Its fee is 200×2=400200 \times 2 = 400.
  • Car 44 parks in the last remaining space 33. Its fee is 800×5=4,000800 \times 5 = 4{,}000.

The total revenue is 600+300+400+4,000=5,300600 + 300 + 400 + 4{,}000 = 5{,}300.

Examples2

  1. Example 1

    Input
    3 4
    2
    3
    5
    200
    100
    300
    800
    3
    2
    -3
    1
    4
    -4
    -2
    -1
    
    Expected output
    5300
    
  2. Example 2

    Input
    2 4
    5
    2
    100
    500
    1000
    2000
    3
    1
    2
    4
    -1
    -3
    -2
    -4
    
    Expected output
    16200