Problem list
Log in
#coci00193 · COCI 2011/2012 Contest 1

Jabuke

Silver V 1.0 s 32 MB

Mirko has recently discovered an old video game. The screen of this game is divided into $N$ columns.

At the bottom of the screen, there is an $M$-columns-wide boat ($M < N$). The player can move this boat left or right during the game, but the boat must remain completely within the screen at all times. The boat initially occupies the leftmost $M$ columns.

Apples are being dropped from the top of the screen. Each apple starts its fall at the top of one of the $N$ columns, falling straight down until it reaches the bottom of the screen. The next apple starts its fall just after the current one has reached the bottom.

An apple is said to be picked up if the boat is placed so that it occupies the column down which the apple is falling when it reaches the bottom. The goal of the game is to pick up all of the apples, in a way that minimizes the distance that the boat must travel.

Input

The first line of input contains two space separated integers $N$ and $M$ ($1 \le M < N \le 10$).

The second line of input contains an integer $J$ ($1 \le J \le 20$), the number of falling apples.

The following $J$ lines contain the column positions of those apples, in the order in which they will fall.

Output

The only line of output must contain the minimal distance that the boat must travel in order to pick up all the apples.

Example 1

Input
5 1
3
1
5
3
Output
6

Example 2

Input
5 2
3
1
5
3
Output
4
Source
COCI 2011/2012 Contest 1
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code