Binary Bros
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 representing how many winning numbers there are. The next line contains
space-seperated integers
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 representing how many tickets the brothers bought. The following line
space-seperated integers
, with each one representing the ticket number of the
th 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 or faster.
Hint: Your output might not fit in a 32-bit integer (). 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