There are students who want to take the shuttle bus from the RDFZ to Renmin University. The
-th student will wait for the bus at the
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
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 , 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 space-separated non-negative integers, and the
-th integer
represents the moment when the
-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 and student
start waiting for the bus at the
-rd minute, wait for
minutes, and take the shuttle bus at the
-rd minute. The shuttle bus returned to RDFZ at the
-th minute.
Student and student
start waiting for the bus at the
-th minute, wait for
minutes, and take the shuttle bus at the
-th minute. The shuttle bus returned to RDFZ at the
-th minute.
Student starts waiting for the bus at the
-th minute, waits for
minutes, and takes the shuttle bus at the
-th minute.
All students have been sent to Renmin University at the -th minute. The total wait time is
minutes.
Sample Input 2
5 5
11 13 1 5 5
Sample Output 2
4
Explanation for Sample 2
Student starts waiting for the bus at the
-st minute, waits for
minutes, and takes the shuttle bus at the
-st minute. The shuttle bus returned to RDFZ at the
-th minute.
Student and student
start waiting for the bus at the
-th minute, wait for
minute, and take the shuttle bus at the
-th minute. The shuttle bus returned to RDFZ at the
-th minute.
Student starts waiting for the bus at the
-th minute and waits
minutes; student
starts waiting for the bus at the
-th minute and waits
minutes. They take the shuttle bus at the
-th minute.
All students have been sent to Renmin University at the -th minute. The total wait time is
minutes.
It can be shown that there is no solution with a total waiting time of less than minutes.
Constraints
For of the data,
,
,
.
For of the data,
,
,
.
For of the data,
,
,
.
There is another of the data,
,
,
.
For of the data,
,
,
.
Problem translated to English by .
Comments