Statues

Time limit: 1s
Memory limit: 512MB
Input:
Output:

<Link /> wants to get his hands on a new C chart, that can only be found on The Isle of <meta>, inside The Temple of <element>. To get inside the Temple, he must solve a puzzle first.

<Link /> must first enter a TT-dimensional plane, therefore every point in space would be described by an array of length T:[x1,x2,x3,,xT]T: [x_1,x_2,x_3, \dots, x_T]. In this plane, there are NN stationary statues numbered from 11 to NN and QQ mobile statues numbered from 11 to QQ. <Link /> can make the following move at most KK times: he can choose any mobile statue and an axis and move that statue by exactly one unit in any direction. That is, the coordinate of such statue will become either [x1,x2,,xi1,,xT][x_1,x_2, \dots,x_i−1,\dots,x_T] or [x1,x2,,xi+1,,xT][x_1,x_2,\dots,x_i+1,\dots, x_T].

To unlock the entrance to The Temple of <element>, he must move the mobile statues in such a way that the sum of the Manhattan distances between every mobile statue and every stationary statue is minimized.

The Manhattan distance between two TT-dimensional points [x1,x2,,xT][x_1,x_2,\dots,x_T] and [y1,y2,,yT][y_1,y_2,\dots,y_T] is defined as:

dist([x1,x2,,xT],[y1,y2,,yT])=i=1Txiyidist([x_1, x_2, \dots, x_T], [y_1, y_2, \dots, y_T]) = \displaystyle \sum_{i = 1}^{T} |x_i - y_i|

Let ss be the array with the coordinates of each stationary statue and mm the array with the coordinates of each mobile statue after making at most KK moves optimally. You are required to compute:

i=1Nj=1Qdist(si,mj)\displaystyle \sum_{i = 1}^{N} \sum_{j = 1}^{Q} dist(s_i, m_j)

Input

The first line of input will contain the integers N,T,KN, T, K which represent the number of stationary statues, the number of dimension and the number of moves that <Link /> can make.

On each of the next NN lines, there will be TT space-separated integers. The ii-th line of these represents the coordinates of the ii-th stationary statue.

On the next line, there will be a single integer QQ representing the number of mobile statues.

On each of the next QQ lines, there will be TT space-separated integers, representing the coordinates of each mobile statue, in a similar fashion as with the stationary statues.

Output

Output a single integer representing the minimum sum of Manhattan distances from every stationary statue to every mobile statue after making at most KK moves.

Constraints and notes

  • 1N,Q100 0001 \leq N, Q \leq 100 \ 000
  • 1T101 \leq T \leq 10
  • 1K10151 \leq K \leq 10^{15}
  • All the coordinates are integers between 00 and 10910^9 inclusive.
  • It is guaranteed that the answer fits in a 64-bit signed integer.
# Points Constraints
1 7 T=Q=1T = Q = 1
2 10 N,Q,K100N, Q, K \leq 100
3 12 N,Q50N, Q \leq 50
4 28 T=1T = 1
5 17 Q=1Q = 1
6 26 No additional restrictions.

Example 1

stdin

3 2 7
8 1
2 0
0 3
2
10 2
2 6

stdout

29

Example 2

stdin

6 4 200
12 1 19 10
45 3 42 44
42 32 40 41
39 12 32 47
35 18 40 20
38 14 25 1
3
34 10 7 9
29 32 21 50
16 36 18 38

stdout

708

Problem info

ID: 1839

Editor: IvanAndrei

Author:

Source: RMI 2023: Day 2 Problem 2

Tags:

RMI 2023

Log in or sign up to be able to send submissions!