#P9115. [IOI 2009] Garage

[IOI 2009] Garage

Background

IOI 2009 D2T1.

Problem Description

A parking garage has NN parking spaces, numbered from 11 to NN in order. Every morning, all parking spaces in the garage are empty. When a car arrives at the garage, the attendant checks whether there is an empty parking space. If there is none, the car waits at the entrance until a new space becomes available. If there is an empty space, the car is parked in the empty space with the smallest number. If multiple cars are waiting at the entrance, they form a queue in the order they arrived. When a parking space becomes available, the first car in the queue (the earliest-arriving car) will be parked in that space.

The parking fee for each car is its weight multiplied by the specific rate of the parking space it uses, and it does not depend on how long the car stays in the garage.

Task: Write a program that, given the specific rate of each parking space, the weight of each car, and the arrival and departure order of all cars, computes the total income of the garage.

Input Format

The first line contains two integers N,MN, M separated by spaces, representing the number of parking spaces and the number of cars.

The next NN lines describe the rates of the parking spaces. Line ss contains an integer RsR_s, meaning the price charged per kilogram for parking space ss.

The next MM lines describe the weights of the cars. Cars are numbered from 11 to MM. Line kk contains an integer WkW_k, meaning the weight of car kk in kilograms.

The next 2M2M lines describe the time order of all arrivals and departures. A positive number ii indicates that car ii arrives at the garage. A negative number i-i indicates that car ii leaves the garage. No car will leave before it arrives. Each car in 1M1\sim M appears exactly twice in the sequence: once for arrival and once for departure. Also, no car will leave before it has been parked in the garage, which means no car leaves while waiting in the queue.

Output Format

Output one integer on a single line, representing the income of the garage manager for today.

3 4
2
3
5
200
100
300
800
3
2
-3
1
4
-4
-2
-1

5300

2 4
5
2
100
500
1000
2000
3
1
2
4
-1
-3
-2
-4

16200

Hint

Sample Explanation

  • Sample 1:

    • Car 33 parks in space 11 and pays 300×2=600300\times 2 = 600 dollars.
    • Car 22 parks in space 22 and pays 100×3=300100\times 3 = 300 dollars.
    • Car 11 parks in space 11 (the space freed by car 33) and pays 200×2=400200\times 2 = 400 dollars.
    • Car 44 parks in space 33 and pays 800×5=4000800\times 5 = 4000 dollars.
  • Sample 2:

    • Car 33 parks in space 11 and pays 1000×5=50001000\times 5 = 5000 dollars.
    • Car 11 parks in space 22 and pays 100×2=200100\times 2 = 200 dollars.
    • Car 22 arrives and waits at the entrance.
    • Car 44 arrives and waits at the entrance, behind car 22 in the queue.
    • When car 11 leaves, car 22 parks in the freed space 22 and pays 500×2=1000500\times 2 = 1000 dollars.
    • When car 33 leaves, car 44 parks in the freed space 11 and pays 2000×5=100002000\times 5 = 10000 dollars.

Constraints and Notes

  • For 40%40\% of the testdata, no car will have to wait in the garage.
  • For 100%100\% of the testdata, 1N1001\leq N\leq 100, 1M20001\leq M\leq 2000, 1Rs1001\leq R_s\leq 100, 1Wk1041\leq W_k\leq 10 ^ 4.

Translated by ChatGPT 5