luogu#P5017. [NOIP 2018 普及组] 摆渡车

    ID: 4035 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2018NOIP 普及组斜率优化

[NOIP 2018 普及组] 摆渡车

Background

NOIP 2018 Junior Group T3.

Problem Description

There are nn students who need to take a ferry shuttle from RDFZ (Renmin University Affiliated High School) to Renmin University. The ii-th student goes to wait for the shuttle at minute tit_i. 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 mm 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 n,mn, m, separated by a space, representing the number of waiting students and the time for one round trip of the shuttle.

The second line contains nn positive integers, separated by spaces. The ii-th non-negative integer tit_i represents the time when the ii-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 11 and 44 start waiting at minute 33, wait 00 minutes, and depart on the shuttle at minute 33. The shuttle returns to RDFZ at minute 44.

Students 22 and 33 start waiting at minute 44, wait 00 minutes, and depart on the shuttle at minute 44. The shuttle returns to RDFZ at minute 55.

Student 55 starts waiting at minute 55, waits 00 minutes, and departs on the shuttle at minute 55. From then on, all students have been delivered to Renmin University. The total waiting time is 00.

Explanation of Sample 2

Student 33 starts waiting at minute 11, waits 00 minutes, and departs on the shuttle at minute 11. The shuttle returns to RDFZ at minute 66.

Students 44 and 55 start waiting at minute 55, wait 11 minute, and depart on the shuttle at minute 66. The shuttle returns to RDFZ at minute 1111.

Student 11 starts waiting at minute 1111 and waits 22 minutes; student 22 starts waiting at minute 1313 and waits 00 minutes. They depart on the shuttle at minute 1313. From then on, all students have been delivered to Renmin University. The total waiting time is 44.

It can be proven that there is no plan with total waiting time less than 44.

Constraints

For 10%10\% of the testdata, n10n \le 10, m=1m = 1, 0ti1000 \le t_i \le 100.

For 30%30\% of the testdata, n20n \le 20, m2m \le 2, 0ti1000 \le t_i \le 100.

For 50%50\% of the testdata, n500n \le 500, m100m \le 100, 0ti1040 \le t_i \le 10^4.

For another 20%20\% of the testdata, n500n \le 500, m10m \le 10, 0ti4×1060 \le t_i \le 4 \times 10^6.

For 100%100\% of the testdata, n500n \le 500, m100m \le 100, 0ti4×1060 \le t_i \le 4 \times 10^6.

Translated by ChatGPT 5