Maged the Recruiter


Submit solution

Points: 1
Time limit: 2.5s
Memory limit: 256M

Author:
Problem type
Allowed languages
C, C++, Java, Python

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 n bright candidates to hire, labelled 1 through n, and he has already optimistically allocated a row of n 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 n candidates in order from 1 to n, so Maged can report it to the board and reinstate his reputation?

Input

The first line contains a single integer n (1 \leq n \leq 10^5), the number of candidates that Maged is going to hire. They are labelled 1 through n.

The next line contains n space-separated integers c_1\ \dots\ c_n (1 \leq c_i \leq 10^9), representing the amount Maged will pay each of the candidates when he hires them.

The next line contains n space-separated integers d_1\ \dots\ d_n (1 \leq d_i \leq n). It is guaranteed that each 1,2,\dots,n occurs exactly once in this line. This represents the seats assigned to each of the n 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 n employees in order 1 to n. 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 0 unhappiness. _ _ 5 _ _.
  • Next, the first seat is filled. There are no hires to the left, so this generates 0 too. 3 _ 5 _ _.
  • Next, the final seat is filled. Again, 0 unhappiness is generated. 3 _ 5 _ 4.
  • Next, the second seat is filled. This will generate 2 unhappiness, as this employee will be paid 1, so there is 1 higher paid employee to the left and 2 to the right. 3 1 5 _ 4.
  • Finally, the fourth seat is filled. This employee is making 2, so there are 2 higher-paid employees to the left and 1 to the right, generating 2 unhappiness.

Thus, overall unhappiness summed is 0+0+2+2=4.

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

There are no comments at the moment.