Binary Bros


Submit solution

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

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

The brothers Jeff and Joey have decided to invest in lottery tickets, as they derived that their strategy has a positive expected value. Unfortunately, they lost the notebook with their working out, and neither of them remember it, but they decided to buy lottery tickets anyway.

Each ticket is assigned an integer, and today they've found out the winning numbers. The lottery system scores numbers based on their index in the winning array, which is given sorted for convenience. Your 'score' is given by the total of the indices of each winning ticket you own. They've bought a lot of tickets, so they want you to add up the score for them.

Input

The first line contains an integer n\ (1 \leq n \leq 10^5) representing how many winning numbers there are. The next line contains n space-seperated integers x_i\ (1 \leq x_i \leq 10^5) detailing each of the winning ticket numbers. These winning numbers are guaranteed to be unique and are given in non-decreasing order.

The following line contains an integer m\ (1 \leq m \leq 10^5) representing how many tickets the brothers bought. The following line m space-seperated integers y_i\ (1 \leq y_i \leq 10^5), with each one representing the ticket number of the ith ticket bought. There might be duplicates in the bought ticket numbers.

Output

A single integer representing the total score gained by the brothers. This is given by the sum of the indices of all winning ticket numbers in the winning array (1-indexed).

Your solution must have a time complexity of O(m\log n) or faster.

Hint: Your output might not fit in a 32-bit integer (10^5 \times 10^5 = 10^{10} > 2^{32}). Consider using one of the 64-bit numeric types in C++ such as uint64_t. This won't impact you if you use Python.

Example

Input 1
6
2 3 6 9 11 15
3
12 5 9
Output 1
4
Explanation 1

The brothers bought 3 tickets with numbers 12, 5, and 9. Of these numbers, only 9 is present in the 6 winning ticket numbers and 9 occurs at the index 4 in the winning array (1-indexed).


Comments

There are no comments at the moment.