Boxes with souvenirs

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 l identical sections. The sections around the circle are numbered consecutively from 0 to l - 1. That is, for i (0iL - 2) sections i and i + 1 are adjacent, and also sections 0 and l - 1 are adjacent. There are n 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 n identical souvenirs. Initially, both Aman and all of the souvenirs are in section 0. Aman should give one souvenir to each team, and after delivering the last souvenir he must return to section 0. Note that some teams may be sitting in section 0.

At any moment, Aman can only carry at most k souvenirs. Aman must pick up souvenirs in section 0, 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.


In this example we have n = 3 teams, Aman’s carrying capacity is k = 2, and the number of sections is l = 8. 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.


The first line contains three integers: the number of teams n, the maximum number of souvenirs Aman can carry at the same time k and the number of sections in the venue of the opening ceremony l (1n107, 1kn, 1l109). The second line contains array positions of length n. The elements of array positions0, ..., positionsn-1 give the section number of all teams. The elements of positions are in non-decreasing order.


Print the smallest number of seconds in which Aman can complete his task.

Time limit 1 second
Memory limit 128 MiB
Input example #1
3 2 8
1 2 5
Output example #1
Source 2015 IOI, Day 1