luogu#P5017. [NOIP 2018 普及组] 摆渡车
[NOIP 2018 普及组] 摆渡车
Background
NOIP 2018 Junior Group T3.
Problem Description
There are students who need to take a ferry shuttle from RDFZ (Renmin University Affiliated High School) to Renmin University. The -th student goes to wait for the shuttle at minute . Only one shuttle is in service, but its capacity can be considered unlimited. The shuttle departs from RDFZ, takes the students on board to Renmin University, and then returns to RDFZ (to pick up other students). One round trip takes a total of minutes (ignore the time for getting on and off). The shuttle must deliver all students to Renmin University.
Kaikai is curious: if he can arrange the shuttle’s departure times arbitrarily, what is the minimum possible sum of all students’ waiting times?
Note: after the shuttle returns to RDFZ, it can depart again immediately.
Input Format
The first line contains two positive integers , separated by a space, representing the number of waiting students and the time for one round trip of the shuttle.
The second line contains positive integers, separated by spaces. The -th non-negative integer represents the time when the -th student arrives at the station.
Output Format
Output one line with one integer, representing the minimum possible sum of all students’ waiting times (in minutes).
5 1
3 4 4 3 5
0
5 5
11 13 1 5 5
4
Hint
Explanation of Sample 1
Students and start waiting at minute , wait minutes, and depart on the shuttle at minute . The shuttle returns to RDFZ at minute .
Students and start waiting at minute , wait minutes, and depart on the shuttle at minute . The shuttle returns to RDFZ at minute .
Student starts waiting at minute , waits minutes, and departs on the shuttle at minute . From then on, all students have been delivered to Renmin University. The total waiting time is .
Explanation of Sample 2
Student starts waiting at minute , waits minutes, and departs on the shuttle at minute . The shuttle returns to RDFZ at minute .
Students and start waiting at minute , wait minute, and depart on the shuttle at minute . The shuttle returns to RDFZ at minute .
Student starts waiting at minute and waits minutes; student starts waiting at minute and waits minutes. They depart on the shuttle at minute . From then on, all students have been delivered to Renmin University. The total waiting time is .
It can be proven that there is no plan with total waiting time less than .
Constraints
For of the testdata, , , .
For of the testdata, , , .
For of the testdata, , , .
For another of the testdata, , , .
For of the testdata, , , .
Translated by ChatGPT 5