Under construction

Still being built. Things may change

EDF

Interview

Time limit1sMemory limit1024 MB

Summary
Given N existing jobs and M jobs arriving later, simulate earliest-deadline-first scheduling with preemption and report whether all jobs finish, and the finish time if they do.
Level

Medium5 of 10

Topics
Greedy, Heap, Simulation
Solved
No attempts yet

Problem

Minku has so much work this year that NN jobs have piled up, and up to MM more jobs are added over time.

Each existing job is given its required time tit_i and deadline did_i. Each added job is given its arrival time wjw_j, required time tjt_j, and deadline djd_j.

Minku starts with the job whose deadline is closest. If a newly added job has an earlier deadline than the job he is working on, he switches to the new job. The interrupted job keeps its progress: its required time is reduced by the time already spent on it, so he can resume it later.

For example, if a job that requires 20 minutes has been worked on for 15 minutes before he switches, he needs only 5 more minutes to finish it.

If two jobs have the same deadline, he does the one with less remaining required time first.

Determine whether Minku can finish all N+MN + M jobs by following this process. Minku focuses on one thing at a time, so he can work on only one job at once. He keeps working until all N+MN + M jobs are finished.

The current time is 00 seconds.

Input

The first line contains the number of jobs NN. (1≤N≤1051 \le N \le 10^5)

Each of the next NN lines contains the required time tit_i and deadline did_i of a job. (0≤ti≤1090 \le t_i \le 10^9, 1≤di≤1091 \le d_i \le 10^9, ti≤dit_i \le d_i)

On the (N+2)(N + 2)-th line, the number of jobs MM that are added during the process is given. (1≤M≤N1 \le M \le N)

Each of the next MM lines contains the arrival time wjw_j, required time tjt_j, and deadline djd_j of an added job. (0≤tj≤1090 \le t_j \le 10^9, 1≤wj,dj≤1091 \le w_j, d_j \le 10^9, wj+tj≤djw_j + t_j \le d_j)

Output

On the first line, print NO if Minku cannot finish all jobs. Print YES if he can.

If he can finish all jobs, print the earliest time at which all jobs are completed on the second line.

Examples2

  1. Example 1

    Input
    3
    10 30
    5 25
    10 35
    2
    5 10 45
    20 5 30
    
    Expected output
    YES
    40
    
  2. Example 2

    Input
    3
    10 30
    5 25
    15 35
    2
    5 10 40
    20 5 30
    
    Expected output
    NO