NOIP '18 Junior P3 - Shuttle Bus

View as PDF

Submit solution

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

Problem type

There are n students who want to take the shuttle bus from the RDFZ to Renmin University. The i-th student will wait for the bus at the t_i minute. Only one shuttle bus is working, but the capacity of the shuttle bus can be regarded as infinite. It will take the students on the bus to Renmin University, and then return to the RDFZ to pick up other students. A round trip takes m minutes and the time for students getting on and off the bus can be ignored. The shuttle bus can leave immediately after returning to the RDFZ.

Kaikai wants to know what is the minimum sum of waiting time for these students if he could arrange the departure time of the shuttle bus.

Input Specification

The first line contains two space-separated positive integers n, m, representing the number of people waiting for the bus and the time the shuttle bus needs to use for a round trip.

The second line contains n space-separated non-negative integers, and the i-th integer t_i represents the moment when the i-th student arrives at the station.

Output Specification

Output an integer, indicating the minimum sum of waiting time for all the students in minutes.

Sample Input 1

5 1
3 4 4 3 5

Sample Output 1

0

Explanation for Sample 1

Student 1 and student 4 start waiting for the bus at the 3-rd minute, wait for 0 minutes, and take the shuttle bus at the 3-rd minute. The shuttle bus returned to RDFZ at the 4-th minute.

Student 2 and student 3 start waiting for the bus at the 4-th minute, wait for 0 minutes, and take the shuttle bus at the 4-th minute. The shuttle bus returned to RDFZ at the 5-th minute.

Student 5 starts waiting for the bus at the 5-th minute, waits for 0 minutes, and takes the shuttle bus at the 5-th minute.

All students have been sent to Renmin University at the 5-th minute. The total wait time is 0 minutes.

Sample Input 2

5 5
11 13 1 5 5

Sample Output 2

4

Explanation for Sample 2

Student 3 starts waiting for the bus at the 1-st minute, waits for 0 minutes, and takes the shuttle bus at the 1-st minute. The shuttle bus returned to RDFZ at the 6-th minute.

Student 4 and student 5 start waiting for the bus at the 5-th minute, wait for 1 minute, and take the shuttle bus at the 6-th minute. The shuttle bus returned to RDFZ at the 11-th minute.

Student 1 starts waiting for the bus at the 11-th minute and waits 2 minutes; student 2 starts waiting for the bus at the 13-th minute and waits 0 minutes. They take the shuttle bus at the 13-th minute.

All students have been sent to Renmin University at the 13-th minute. The total wait time is 4 minutes.

It can be shown that there is no solution with a total waiting time of less than 4 minutes.

Constraints

For 10\% of the data, n \le 10, m = 1, 0 \le t_i \le 100.

For 30\% of the data, n \le 20, m \le 2, 0 \le t_i \le 100.

For 50\% of the data, n \le 500, m \le 100, 0 \le t_i \le 10^4.

There is another 20\% of the data, n \le 500, m \le 10, 0 \le t_i \le 4 \times 10^6.

For 100\% of the data, n \le 500, m \le 100, 0 \le t_i \le 4 \times 10^6.

Problem translated to English by Tommy_Shan.


Comments

There are no comments at the moment.