Bribe the Prisoners (Large)

Given prison cells in a row and a set of cells to release one per day, choose the release order that minimizes total bribes paid to prisoners who hear the news.

Medium7Dynamic programmingDivide and conquerIntervalsRecursionNo attempts yetTime limit5sMemory limit512 MB

Problem

A kingdom keeps its prison cells, numbered 1 to PP, in one straight line. Cells ii and i+1i+1 are adjacent, and the prisoners in two adjacent cells are called neighbours. The wall between two adjacent cells has a window, so neighbours talk to each other.

The prison stays quiet until a prisoner is released. When that happens, the neighbours of the released prisoner learn about it, and each one passes the news to his other neighbour. That prisoner passes it on to his own other neighbour, and so on, until the news reaches a prisoner who has no other neighbour, because he sits in cell 1, or in cell PP, or the cell on his other side is empty. A prisoner who learns that someone was released smashes everything in his cell, unless you give him one gold coin. So releasing the prisoner in cell AA forces you to bribe every prisoner on both sides of cell AA, going outward until you reach cell 1, cell PP, or an empty cell.

Every cell starts with exactly one prisoner, and you release only one prisoner per day. Given the cell numbers of the QQ prisoners to release over QQ days, find the smallest total number of gold coins you need when you may choose the release order freely.

A bribe lasts for that one day only. A prisoner bribed yesterday must be bribed again if he hears about another release today.

Input

The first line has the number of test cases, NN. NN test cases follow. Each case takes two lines. The first line is

P Q

where PP is the number of prison cells and QQ is the number of prisoners to release. The second line has the QQ cell numbers of those prisoners, all different, space separated and sorted in ascending order.

Limits

  • 1N1001 \le N \le 100
  • 1P100001 \le P \le 10000
  • 1Q1001 \le Q \le 100
  • QPQ \le P
  • every cell number is between 1 and PP, inclusive

Output

For each test case print one line in the format

Case #X: C

where XX is the case number, starting from 1, and CC is the minimum number of gold coins needed for bribes.

Hint

Take P=20P = 20 with releases in cells 3, 6 and 14. Releasing cell 14 first, then cell 6, then cell 3 costs 19 + 12 + 4 = 35 gold coins. Releasing cell 6 first costs 19 + 4 + 13 = 36, which is worse.