This page is still under construction.

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

Ski Lessons

Time limit1sMemory limit128 MB

Summary
Given ski lessons that overwrite Bessie's skill at fixed start times and slopes with skill and time costs, maximize the number of runs by time T.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Sorting, Binary search
Solved
No attempts yet

Problem

Farmer John wants to take Bessie skiing in Colorado. Sadly, Bessie is not a very good skier.

The ski resort offers SS ski lessons throughout the day (0≤S≤1000 \le S \le 100). Lesson ii starts at time MiM_i and lasts for LiL_i units of time (1≤Mi≤100001 \le M_i \le 10000, 1≤Li≤100001 \le L_i \le 10000). When lesson ii finishes (i.e. at time Mi+LiM_i + L_i), Bessie's skill level becomes AiA_i (1≤Ai≤1001 \le A_i \le 100). This is an absolute value that overwrites her skill, not an increment.

The resort has NN ski slopes (1≤N≤100001 \le N \le 10000). Skiing down slope ii once takes DiD_i units of time (1≤Di≤100001 \le D_i \le 10000) and requires a skill level of at least CiC_i to descend safely (1≤Ci≤1001 \le C_i \le 100). Bessie may descend slope ii only when her skill level is at least CiC_i. She may ski any slope as many times as she likes, and each descent counts as one run.

Bessie can spend her time skiing, taking lessons, or resting (sipping hot cocoa), but she can only do one thing at a time. A lesson starts at its fixed time MiM_i, so to attend it she must be free (not in the middle of a descent or another lesson) at exactly time MiM_i.

Bessie starts the day at time 00 with skill level 11 and must leave the resort by time TT (1≤T≤100001 \le T \le 10000); that is, she must complete the descent of her last slope without exceeding time TT.

Find the maximum number of runs Bessie can complete within the time limit.

Input

  • Line 1: three space-separated integers TT, SS, and NN
  • Next SS lines: line ii describes lesson ii with three space-separated integers MiM_i, LiL_i, and AiA_i
  • Next NN lines: line ii describes slope ii with two space-separated integers CiC_i and DiD_i

Output

Print a single integer on its own line: the maximum number of runs Bessie can complete within the time limit.

Hint

One optimal strategy is: ski the slope with C=1C = 1, D=3D = 3 once (time 0→30 \to 3), take the lesson that starts at time 33 to raise the skill level to 55 (time 3→53 \to 5), then ski the slope with C=4C = 4, D=1D = 1 five times before time runs out (time 5→105 \to 10), for a total of 66 runs.

Examples3

  1. Example 1

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

    Input
    10 0 1
    1 2
    
    Expected output
    5
    
  3. Example 3

    Input
    5 0 1
    2 1
    
    Expected output
    0