#P6403. [COCI 2014/2015 #2] STUDENTSKO

[COCI 2014/2015 #2] STUDENTSKO

Description

The annual University of Zagreb student team table tennis tournament will be held next Saturday! Each team consists of kk students. nn excited students are standing in a queue, waiting to register. Krešo is working at the registration desk. He really does not want to do his job, so he decided not to let the students choose their teams. He decided that the first team will be made up of the first kk students in the queue, the second team will be made up of the next kk students, the third team will be made up of the next kk students, and so on. (n0(modk)n\equiv 0\pmod k, so nobody is left over.)

Ante estimates each player's skill with an integer. He wants the first team to have the weakest kk players, the second team to have the second weakest kk players, and so on, and the last team to have the strongest kk players.

Krešo has just taken a break, and Ante decided to rearrange the students in the queue to achieve his goal. He rearranges the students by telling a student to step out of the queue and line up behind another student, or to go to the front of the queue. Moving one student takes one minute. Krešo may come back from his break at any time, so Ante needs to achieve his goal as quickly as possible. Help Ante determine the minimum number of minutes needed to achieve his goal.

Input Format

The first line contains integers nn and kk, satisfying nmodk=0n\bmod k=0.

The second line contains nn space-separated integers viv_i, where viv_i is the skill level of the ii-th player standing in the queue.

All contestants have distinct skill levels.

Output Format

Print one line: the minimum number of minutes required to move the students.

4 1
9 12 5 13
1
6 2
16 2 1 7 5 10
1
6 3
7 9 8 3 6 5 
3

Hint

Explanation for Sample 3

Ante should move the students with skill levels 5,65,6 and 33 to the front of the queue, which takes three minutes.

Constraints

  • For 30%30\% of the testdata, 1n201\le n\le 20.
  • For 100%100\% of the testdata, 1kn5×1031\le k\le n\le 5\times 10^3.

For all valid viv_i, 1vi1091\le v_i\le 10^9.

Note

This problem is translated from COCI2014-2015 CONTEST #2 T3 STUDENTSKO.

Translated by ChatGPT 5