Under construction

Still being built. Things may change

DJ Gigs

Interview

Time limit8sMemory limit1024 MB

Summary
Doug starts at venue 1 at time 0 and picks non-overlapping gigs, accounting for road travel times between venues, to maximize total pay.
Level

Medium5 of 10

Topics
Dynamic programming, Shortest path, Sorting, Binary search
Solved
No attempts yet

Problem

Doug James is an up-and-coming DJ from Graphland who has had a tough time making it big. That changed when his latest EP Wiggly Waves became the first album in history to go both Platinum and Uranium. With his newfound popularity, Doug (a.k.a. DJ Polygon) has a problem most artists would envy: he must decide which of his many gig offers to accept.

There are KK venues in Graphland, connected by RR roads. The venues are numbered 11 to KK. Doug's house is venue 11, and he is ready to leave or perform at time t=0t = 0.

Doug has GG gig offers. The ii-th gig is at venue ViV_i, runs during the time interval [Si,Ei)[S_i, E_i) (inclusive start, exclusive end), and pays MiM_i cryptocents. Doug cannot take multiple gigs at the same time, and he cannot take gigs while he is traveling between venues.

Doug is overwhelmed by his fame and the many gig requests, so he wants your help making as much money as possible.

Input

The first line contains three integers GG, KK, and RR: the number of gigs, the number of venues, and the number of roads. These satisfy 1≤G≤2000001 \leq G \leq 200000, 1≤K≤1001 \leq K \leq 100, and 0≤R≤min⁡(4000,K(K−1)/2)0 \leq R \leq \min(4000, K(K-1)/2).

Each of the next RR lines contains three integers AiA_i, BiB_i, and TiT_i, describing the ii-th bidirectional road. It connects venues AiA_i and BiB_i, and it takes TiT_i time to travel the road in either direction. The values satisfy 1≤Ai,Bi≤K1 \leq A_i, B_i \leq K, Ai≠BiA_i \neq B_i, and 1≤Ti≤10000001 \leq T_i \leq 1000000. There is at most one road between any pair of venues.

Each of the next GG lines contains four integers ViV_i, SiS_i, EiE_i, and MiM_i. The ii-th gig runs from time SiS_i (inclusive) to EiE_i (exclusive) at venue ViV_i and pays MiM_i cryptocents. These values satisfy 0≤Si<Ei≤10000000000 \leq S_i < E_i \leq 1000000000 and 1≤Mi≤10000001 \leq M_i \leq 1000000.

Output

Output a single integer: the maximum number of cryptocents Doug can earn by taking the right gigs.

Hints

In the first sample, there are two gigs at venue 11 and one gig at venue 22. Doug can either play both gigs at venue 11 for 1111 cryptocents, or spend the first 1010 units of time traveling to venue 22 and play that gig for 3333 cryptocents. He chooses the latter.

In the second sample, Doug earns the most by staying at venue 11 and playing both gigs for 7070 cryptocents.

Examples2

  1. Example 1

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

    Input
    3 2 1
    1 2 10
    1 4 6 30
    1 6 10 40
    2 10 30 50
    
    Expected output
    70