Under construction

Still being built. Things may change

Cool Desks

Time limit2sMemory limit512 MB

Summary
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 nn two-seat desks.

Desks come in kk types, and each type fixes a size. A desk of type ii suits students whose height is in the range from LiL_i to RiR_i 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 Li=100L_i = 100 and Ri=120R_i = 120, 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.

mm groups of students take turns using the classroom. Each group has 2n2n 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 nn 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 mm, nn, and kk (1≤m,n≤200 0001 \le m, n \le 200\,000; 1≤m⋅n≤200 0001 \le m \cdot n \le 200\,000; 2≤k≤200 0002 \le k \le 200\,000): the number of groups, the number of desks to buy, and the number of desk types.

Each of the next kk lines contains two integers LiL_i and RiR_i (1≤Li≤Ri≤1091 \le L_i \le R_i \le 10^9), the height range suited to desks of type ii.

Each of the next mm lines describes a group. It contains 2n2n integers h1,h2,…,h2nh_1, h_2, \ldots, h_{2n} (1≤hi≤1091 \le h_i \le 10^9), the heights of the students in that group.

Output

Print PP, 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.

Examples3

  1. Example 1

    Input
    1 2 2
    5 25
    50 90
    60 5 10 40
    
    Expected output
    10
    
  2. Example 2

    Input
    2 3 3
    200 400
    300 500
    100 600
    300 330 440 40 30 300
    150 250 350 450 550 300
    
    Expected output
    130
    
  3. Example 3

    Input
    1 3 4
    10 100
    200 200
    10 100
    300 1000
    5 10 20 15 200 90
    
    Expected output
    105