A downtown parking lot has $N$ parking spaces numbered from $1$ to $N$. 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 $M$ 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.
A car never leaves without having entered. Every car from $1$ to $M$ enters exactly once and leaves exactly once. A car waiting at the entrance never leaves without parking.
Print a single integer on one line: the total revenue the lot earns over the day.
For example, suppose the space rates are $2, 3, 5$; the car weights are $200, 100, 300, 800$; and the enter/leave order is $3, 2, -3, 1, 4, -4, -2, -1$. Then:
The total revenue is $600 + 300 + 400 + 4{,}000 = 5{,}300$.