Reading List
Time limit1sMemory limit128 MB
Total lifted books over a sequence of assignments, where each assigned book moves to the top of the tower.
- Level
Medium5 of 10
- Topics
- Array, Sorting, Prefix sum, Simulation
- Solved
- No attempts yet
Problem
Mirko is a model student, yet he has always struggled with his required reading. This year he decided to put an end to it. He got hold of all books that the teacher can assign as required reading, numbered from to . He stacked them all into a single tower from the smallest number to the largest, so book is at the top of the tower.
Each time the teacher assigns book , Mirko has to pull it out of the tower to read it. He does this by lifting all the books above book , pulling out book , and putting the lifted books back down on the tower. After Mirko finishes reading book , he puts it back on top of the tower. Book also counts as a lifted book.

Mirko reads the book numbered
For example, look at the tower in the figure. If the first assigned book is book , Mirko lifts three books, numbered , and . He then puts books and back down. After he reads book , he places it on top of the tower.
You are given the sequence of books the teacher will assign. Compute how many books Mirko lifts in total this school year to read all the assigned books.
Input
The first line contains two positive integers and (), the number of books and the number of assignments.
The second line contains positive integers (), the assigned books in order.
Some books may never be assigned. A book may also be assigned more than once, in which case Mirko must read it again.
Output
Print a single positive integer on the first line: the total number of books Mirko lifts.
Hint
Consider books assigned in the order , , . Listing the tower from top to bottom, it changes as follows.
- : only book is lifted.
- : books , and are lifted.
- : books , and are lifted.
In total Mirko lifts books.