Under construction

Still being built. Things may change

Convenience Zone

Interview

Time limit2sMemory limit1024 MB

Summary
Given rooms and convenience stores on an N by M grid, find the minimum of (Manhattan distance to the nearest store) times monthly rent over all rooms.
Level

Easy3 of 10

Topics
BFS, Matrix
Solved
No attempts yet

Problem

Hyeonseong, worn out from a 4-hour round trip commute, is looking for a room to rent. He only cares about the monthly rent and the distance to the nearest convenience store.

He gives every room on the map a convenience zone score and picks the room with the lowest score. The score is calculated as follows.

Convenience zone score = (distance from the room to the nearest convenience store × monthly rent)

The map is an N×MN \times M grid. The cell in row xx and column yy is written as (x,y)(x,y). If a room is at (a,b)(a,b) and a convenience store is at (c,d)(c,d), the distance between them is ∣a−c∣+∣b−d∣|a-c|+|b-d|.

Hyeonseong chose the room with the lowest convenience zone score. What is that score?

Input

The first line contains NN and MM, the map size, followed by RR, the number of rooms, and CC, the number of convenience stores, separated by spaces.

From the second line, the next RR lines each contain three integers aia_i, bib_i, pip_i separated by spaces. This means the ii-th room is at (ai,bi)(a_i, b_i) and its monthly rent is pip_i.

From line R+2R+2, the next CC lines each contain two integers cjc_j and djd_j separated by a space. This means the jj-th convenience store is at (cj,dj)(c_j, d_j).

All rooms and convenience stores are at different positions. In other words, each position holds at most one room or one convenience store.

Output

Print the convenience zone score of the room Hyeonseong chose, on the first line.

Constraints

  • 2≤N≤1,0002 \le N \le 1{,}000
  • 2≤M≤1,0002 \le M \le 1{,}000
  • 2≤R+C≤min⁡(NM,5×105)2 \le R+C \le \min(NM, 5\times10^5)
  • 1≤ai,cj≤N1 \le a_i, c_j \le N
  • 1≤bi,dj≤M1 \le b_i, d_j \le M
  • 1≤pi≤1001 \le p_i \le 100
  • There is at least one room and at least one convenience store.

Hint

In the first example, the room with rent 3 and a convenience store 2 steps away has the lowest score, 6.

In the second example, the room with rent 2 and a convenience store 2 steps away has the lowest score, 4.

Examples2

  1. Example 1

    Input
    5 5 2 1
    1 1 2
    4 5 3
    4 3
    
    Expected output
    6
    
  2. Example 2

    Input
    5 5 2 3
    1 1 2
    4 5 3
    2 2
    2 4
    4 3
    
    Expected output
    4