Cool Desks
Time limit2sMemory limit512 MB
Buy n two-seat desks from k height-range types and seat each group's 2n students in pairs to minimize the total discomfort across all groups.
- Level
Hard8 of 10
- Topics
- Greedy, Sorting, Binary search
- Solved
- No attempts yet
Problem
The new classroom at Innopolis School needs two-seat desks.
Desks come in types, and each type fixes a size. A desk of type suits students whose height is in the range from to inclusive. Students outside that range find the desk uncomfortable. The discomfort of such a student is the absolute difference between the student's height and the nearest boundary of the desk's range. If the desk suits the student, the discomfort is zero.
For example, if and , the discomfort for a student of height 80 is 20, for a student of height 130 it is 10, and for a student of height 105 it is 0.
groups of students take turns using the classroom. Each group has students, and the height of every student is known. The desks are placed in the classroom, and in each group every desk seats exactly two students. Buy desks and seat the students of each group so that the total discomfort of all students using the classroom is minimal.
Input
The first line contains three integers , , and (; ; ): the number of groups, the number of desks to buy, and the number of desk types.
Each of the next lines contains two integers and (), the height range suited to desks of type .
Each of the next lines describes a group. It contains integers (), the heights of the students in that group.
Output
Print , the minimum total discomfort that can be reached by buying desks and seating the students of each group optimally.
Hint
In the first example there is only one group. Buy one desk of each type. Seat the students of heights 5 and 10 at the desk of type 1, and the students of heights 40 and 60 at the desk of type 2. Then only the student of height 40 is uncomfortable, with discomfort 10.