Maged the Recruiter
Maged used to be the CEO of AUCPL inc. However, when an employee posted mocking memes about him on Lark, he got angry and fired the whole department. The board was not very happy about this, as that was one of the most important departments, Dead Revenue, so they have demoted Maged to be a recruiter and tasked him with hiring new employees to revitalise the department.
Maged has done his research and found bright candidates to hire, labelled
through
, and he has already optimistically allocated a row of
desks to them, assigning each candidate a desk. He is set on hiring them, and has found how much he will have to pay each to get them to join. However, each new hire will be upset if they find out that people who have already been hired make more than them, generating unhappiness scores.
Specifically, for each hire, the unhappiness generated is equal to the number of people already hired being paid more than them to the left of them multiplied by the amount of people already hired being paid more than them to the right of them. Can you calculate the sum of all unhappiness scores after hiring all candidates in order from
to
, so Maged can report it to the board and reinstate his reputation?
Input
The first line contains a single integer (
), the number of candidates that Maged is going to hire. They are labelled
through
.
The next line contains space-separated integers
(
), representing the amount Maged will pay each of the candidates when he hires them.
The next line contains space-separated integers
(
). It is guaranteed that each
occurs exactly once in this line. This represents the seats assigned to each of the
employees. Specifically, the integer in this line is their employee number, as well as the order they will be hired, and their position in this line defines their seat position in the row.
Output
Output the sum of all the unhappiness scores obtained by hiring all employees in order
to
. The unhappiness score of each hire is defined as the number of already-hired employees being paid more to the left times the number of already-hired employees being paid more to the right of the new hire.
Example
Input 1
5
5 3 4 1 2
2 4 1 5 3
Output 1
4
We can walk through the hires step by step. Initially all seats are empty: _ _ _ _ _.
- Firstly, the third seat is filled. There are no hires to either side, so this generates
unhappiness.
_ _ 5 _ _. - Next, the first seat is filled. There are no hires to the left, so this generates
too.
3 _ 5 _ _. - Next, the final seat is filled. Again,
unhappiness is generated.
3 _ 5 _ 4. - Next, the second seat is filled. This will generate
unhappiness, as this employee will be paid
, so there is
higher paid employee to the left and
to the right.
3 1 5 _ 4. - Finally, the fourth seat is filled. This employee is making
, so there are
higher-paid employees to the left and
to the right, generating
unhappiness.
Thus, overall unhappiness summed is .
Input 2
3
3 1 2
1 2 3
Output 2
0
Input 3
5
100 99 50 100 5
1 3 5 4 2
Output 3
5
Comments