Food Festival
Time limit1sMemory limit512 MB
Given each dish's student count and each chef's cooking time per dish, assign dishes to chefs and set their order to minimize the total waiting time of all students.
Problem
City CZ is hosting a large food festival to welcome students from across the nation. Little M loves trying new flavors and does not want to miss any dish. He wants no student to wait for a dish that is already on someone else's table, so he studies how to order the preparation of dishes to minimize the students' waiting time.
The festival has kinds of dishes. Each student picks one of these kinds when ordering. There are chefs. After all students have ordered, each chef is assigned a list of dishes to prepare. Then all chefs start cooking at the same time. Each chef cooks in the assigned order and can make only one portion at a time.
Chefs need different amounts of time for the same dish. Let be the time chef needs to prepare dish . Dishes are numbered 1 to , and chefs are numbered 1 to .
A student's waiting time is the time from when all chefs start cooking until the student's dish is finished. If the student's dish is the -th dish that some chef prepares, the waiting time is the sum of the preparation times of that chef's first dishes. The total waiting time is the sum of the waiting times of all students.
exactly students ordered dish . Find the minimum possible total waiting time.
Input
The first line contains two positive integers and .
The second line contains positive integers , where is the number of students who ordered dish .
Each of the next lines contains nonnegative integers. The -th integer on the -th line is . Adjacent numbers on a line are separated by a single space, and no line has trailing spaces.
Output
Print one line containing the minimum possible total waiting time.
Constraints
, , , and , where is the total number of students who ordered.