IOI '09 - Plovdiv, Bulgaria
A parking garage has N parking spaces, numbered from 1 to N inclusive. The garage opens empty each morning and operates in the following way throughout the day. Whenever a car arrives at the garage, the attendants check whether there are any parking spaces available. If there are none, then the car waits at the entrance until a parking space is released. If a parking space is available, or as soon as one becomes available, the car is parked in the available parking space. If there is more than one available parking space, the car will be parked at the space with the smallest number. If more cars arrive while some car is waiting, they all line up in a queue at the entrance, in the order in which they arrived. Then, when a parking space becomes available, the first car in the queue (i.e., the one that arrived the earliest) is parked there.
The cost of parking in dollars is the weight of the car in kilograms multiplied by the specific rate of its parking space. The cost does not depend on how long a car stays in the garage.
The garage operator knows that today there will be M cars coming and he knows the order of their arrivals and departures. Help him calculate how many dollars his revenue is going to be today.
Write a program that, given the specific rates of the parking spaces, the weights of the cars and the order in which the cars arrive and depart, determines the total revenue of the garage in dollars.
InputYour program must read from standard input the following data:
- The first line contains the integers N (1 ≤ N ≤ 100) and M (1 ≤ M ≤ 2000), separated by a space.
- The next N lines describe the rates of the parking spaces. The sth of these lines contains a single integer Rs (1 ≤ Rs ≤ 100), the rate of parking space number s in dollars per kilogram.
- The next M lines describe the weights of the cars. The cars are numbered from 1 to M inclusive in no particular order. The kth of these M lines contains a single integer Wk (1 ≤ Wk ≤ 10000), the weight of car k in kilograms.
- The next 2M lines describe the arrivals and departures of all cars in chronological order. A positive integer i indicates that car number i arrives at the garage. A negative integer -i indicates that car number i departs from the garage. No car will depart from the garage before it has arrived, and all cars from 1 to M inclusive will appear exactly twice in this sequence, once arriving and once departing. Moreover, no car will depart from the garage before it has parked (i.e., no car will leave while waiting on the queue).
OutputYour program must write to standard output a single line containing a single integer: the total number of dollars that will be earned by the garage operator today.
Sample Input 1
3 4 2 3 5 200 100 300 800 3 2 -3 1 4 -4 -2 -1
Sample Output 1
Explanation of Sample Case 1Car number 3 goes to space number 1 and pays 300 * 2 = 600 dollars.
Car number 2 goes to space number 2 and pays 100 * 3 = 300 dollars.
Car number 1 goes to space number 1 (which was released by car number 3) and pays 200 * 2 = 400 dollars.
Car number 4 goes to space number 3 (the last remaining) and pays 800 * 5 = 4,000 dollars.
Sample Input 2
2 4 5 2 100 500 1000 2000 3 1 2 4 -1 -3 -2 -4
Sample Output 2
Explanation of Sample Case 2Car number 3 goes to space number 1 and pays 1,000 * 5 = 5,000 dollars.
Car number 1 goes to space number 2 and pays 100 * 2 = 200 dollars.
Car number 2 arrives and has to wait at the entrance.
Car number 4 arrives and has to wait at the entrance behind car number 2.
When car number 1 releases its parking space, car number 2 parks there and pays 500 * 2 = 1,000 dollars.
When car number 3 releases its parking space, car number 4 parks there and pays 2,000 * 5 = 10,000 dollars.
Note on gradingFor a number of tests worth 40 points there will always be at least one available parking space for every arriving car. In these cases no car will ever have to wait for a space.
Point Value: 10
Time Limit: 2.00s
Memory Limit: 16M
Added: Feb 19, 2010
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3