Cipele
시간 제한2초메모리 제한2048 MB
매일 요청되는 신발 순서가 주어질 때, 용량 m인 복도에 둘 신발과 옷장 맨 위로 보낼 신발을 정해 총 꺼내는 시간을 최소화한다.
문제
Lana has pairs of shoes, labeled from to . All the shoes are stored in a long wardrobe. The shoes labeled are initially at the top of the wardrobe (close to the door), while the shoes labeled are at the bottom (far from the door).
On the -th of the next days, Lana will want to wear the shoes labeled . To retrieve a pair of shoes from the wardrobe, she first needs to remove all the pairs that are closer to the door before she can take out the desired pair. Once she retrieves the desired shoes, she will return other shoes to the wardrobe in the same order as they were before. Retrieving one pair of shoes from the wardrobe takes second, and returning shoes to the wardrobe takes no additional time.
At the end of the day, Lana will take off the shoes and either:
- return them to the top of the wardrobe, or
- leave them in the hallway, if there is space available in the hallway.
The hallway can hold up to pairs of shoes. Additionally, Lana can move any shoes from the hallway to the top of the wardrobe at any time (except when in the process of taking out the desired pair). If the desired pair of shoes is already in the hallway at the beginning of the day, Lana can immediately wear them without spending any time retrieving them.
Lana is very busy and wants to minimize the total time spent retrieving shoes from the wardrobe. Help her determine the minimum time she can spend retrieving shoes over the next days!
입력
The first line contains integers , and (, , ) number of shoes, number of spaces in the hallway and number of days.
The second line contains integers () shoe label that Lana wants to wear on the -th day.
출력
In the first and only line, output the minimum time required to retrieve the shoes in all days.
힌트
Clarification of the first example:
On the first day, Lana will take the shoes labeled out of the wardrobe. This action will take her seconds. At the end of the day, she will leave those shoes in the hallway and keep them there indefinitely.
Now, whenever she needs to retrieve the shoes labeled from the wardrobe, it will take her second. However, if she needs the shoes labeled , she can immediately wear them from the hallway without spending any time.
The total time she will spend retrieving shoes is: seconds.