#P9115. [IOI 2009] Garage
[IOI 2009] Garage
Background
IOI 2009 D2T1.
Problem Description
A parking garage has parking spaces, numbered from to 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 separated by spaces, representing the number of parking spaces and the number of cars.
The next lines describe the rates of the parking spaces. Line contains an integer , meaning the price charged per kilogram for parking space .
The next lines describe the weights of the cars. Cars are numbered from to . Line contains an integer , meaning the weight of car in kilograms.
The next lines describe the time order of all arrivals and departures. A positive number indicates that car arrives at the garage. A negative number indicates that car leaves the garage. No car will leave before it arrives. Each car in 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 parks in space and pays dollars.
- Car parks in space and pays dollars.
- Car parks in space (the space freed by car ) and pays dollars.
- Car parks in space and pays dollars.
-
Sample 2:
- Car parks in space and pays dollars.
- Car parks in space and pays dollars.
- Car arrives and waits at the entrance.
- Car arrives and waits at the entrance, behind car in the queue.
- When car leaves, car parks in the freed space and pays dollars.
- When car leaves, car parks in the freed space and pays dollars.
Constraints and Notes
- For of the testdata, no car will have to wait in the garage.
- For of the testdata, , , , .
Translated by ChatGPT 5