This page is still under construction.

Parts of this page are still being built. What you see may change.

Gourmet Grazers

Interview

Time limit1sMemory limit128 MB

Summary
Assign each cow a distinct grass type whose price and greenness both meet her minimums, minimizing total price; print -1 if impossible.
Level

Medium6 of 10

Topics
Greedy, Sorting, Binary search, Implementation
Solved
No attempts yet

Problem

Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass for each of his NN (1≤N≤1051 \le N \le 10^5) cows.

Each cow ii demands grass whose price is at least AiA_i (1≤Ai≤1091 \le A_i \le 10^9) and whose greenness score is at least BiB_i (1≤Bi≤1091 \le B_i \le 10^9). The store has MM (1≤M≤1051 \le M \le 10^5) different types of grass available; each grass jj has a price CjC_j (1≤Cj≤1091 \le C_j \le 10^9) and a greenness score DjD_j (1≤Dj≤1091 \le D_j \le 10^9). Of course, no cow would sacrifice her individuality, so no two cows can be given the same kind of grass (each type of grass may be assigned to at most one cow).

Help Farmer John satisfy the cows' expensive gourmet tastes while spending as little money as necessary.

Input

  • Line 1: Two space-separated integers NN and MM.
  • Next NN lines: line ii contains two space-separated integers AiA_i and BiB_i.
  • Next MM lines: line jj contains two space-separated integers CjC_j and DjD_j.

Output

  • A single integer: the minimum cost to satisfy all the cows. If it is impossible, output −1-1.

Hint

  • In the first example, cow 1 eats a grass costing 2, cow 2 a grass costing 4, cow 3 a grass costing 2, and cow 4 a grass costing 4, for a total cost of 2+4+2+4=122+4+2+4=12.

Examples2

  1. Example 1

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

    Input
    1 1
    1 1
    1 1
    
    Expected output
    1