Under construction

Still being built. Things may change

Convenience Store Zone

Time limit3sMemory limit1024 MB

Summary
Given rooms with rents and stores on a grid, find the minimum of (Manhattan distance to the nearest store) times rent over all rooms.
Level

Medium6 of 10

Topics
BFS, Matrix, Intervals
Solved
No attempts yet

Problem

A student who commutes four hours a day round trip wants to find a room near a convenience store.

He chooses a room using only the monthly rent and the distance to the nearest convenience store. To find the room he likes most, he gives every room on the map a convenience score and picks the room with the lowest score. The score is calculated as follows.

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

The map is a grid of N×MN \times M cells. The cell in row xx and column yy is written as (x,y)(x,y). When 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|.

What is the convenience score of the room he picked, the one with the lowest score?

Input

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

The next RR lines each contain three integers aia_i, bib_i, pip_i, separated by spaces. These mean that the ii-th room is at (ai,bi)(a_i, b_i) and its monthly rent is pip_i.

The next CC lines each contain two integers cjc_j, djd_j, separated by spaces. These mean that the jj-th convenience store is at (cj,dj)(c_j, d_j).

All rooms and convenience stores are at different positions. A cell holds at most one room or one convenience store.

Output

Print the convenience score of the room he picked on the first line.

Constraints

  • 2≤N≤5×1052 \le N \le 5\times10^5
  • 2≤M≤5×1052 \le M \le 5\times10^5
  • 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≤1051 \le p_i \le 10^5
  • There is at least one room and at least one convenience store.

Hint

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