## IOI '15 P1 - Boxes with Souvenirs (Standard I/O)

View as PDF

Points: 15 (partial)
Time limit: 2.0s
Memory limit: 256M

Problem type

The last act of the IOI 2015 opening ceremony is in progress. During the opening ceremony, each team was supposed to receive a box with a souvenir from the host. However, all volunteers are so fascinated by the ceremony that they completely forgot about the souvenirs. The only person who remembers about the souvenirs is Aman. He is an enthusiastic volunteer and he wants the IOI to be perfect, so he wants to deliver all the souvenirs in the least amount of time.

The venue of the opening ceremony is a circle divided into identical sections. The sections around the circle are numbered consecutively from to . That is, for , sections and are adjacent, and also sections and are adjacent. There are teams at the venue. Each team is sitting in one of the sections. Each section may contain arbitrarily many teams. Some sections may even be empty.

There are identical souvenirs. Initially, both Aman and all of the souvenirs are in section . Aman should give one souvenir to each team, and after delivering the last souvenir he must return to section . Note that some teams may be sitting in section .

At any moment, Aman can only carry at most souvenirs. Aman must pick up souvenirs in section , and this takes him no time. Each souvenir must be carried until it is delivered to one of the teams. Whenever Aman carries one or more souvenirs and reaches a section with a team that has not received a souvenir yet, he may give that team one of the souvenirs he carries. This also happens instantly. The only thing that takes time is movement. Aman can move around the circular venue in both directions. Moving to an adjacent section (either clockwise or counterclockwise) takes him exactly one second, regardless of how many souvenirs he carries.

Your task is to find the smallest number of seconds Aman needs to deliver all souvenirs and then return to his initial position.

#### Example

In this example we have teams, Aman's carrying capacity is , and the number of sections is . The teams are located in sections 1, 2, and 5.

One of the optimal solutions is shown in the picture above. In his first trip Aman takes two souvenirs, delivers one to the team in section 2, then the other to the team in section 5, and finally he returns to section 0. This trip takes 8 seconds. In his second trip Aman brings the remaining souvenir to the team in section 1 and then returns to section 0. He needs another 2 seconds to do this. Thus, the total time is 10 seconds.

Your task is, given , , , and the positions of all teams, compute the smallest number of seconds Aman needs to deliver all the souvenirs and return to section 0.

#### Input Specification

Line of input will contain three space-separated integers , , and , respectively representing the number of teams, the maximum number of souvenirs Aman can carry at the same time, and the number of sections in the venue of the opening ceremony.
Line of input will contain positions, an array of length . The space separated integers give the section number of all teams. The elements of positions are in non-decreasing order.

#### Output Specification

You should output a single integer – the smallest number of seconds in which Aman can complete his task.

#### Sample Input

3 2 8
1 2 5

#### Sample Output

10