Under construction

Still being built. Things may change

Interrail Pass

Interview

Time limit1sMemory limit1024 MB

Summary
Given travel days with individual fares and pass types that cover the first d travel days of a p-day window, find the minimum total cost to cover all travel days.
Level

Medium5 of 10

Topics
Dynamic programming, Binary search
Solved
No attempts yet

Problem

Interrail passes are a fun and cheap way to see more of Europe, especially when you combine your train trip with Businesslike And Penny-saving Computation. You want to find the cheapest way to pay for your planned travels. You plan to take the train on nn travel days, which are not necessarily consecutive. Each day has its own individual fare. Buying some interrail passes may save money.

There are kk types of interrail passes with different costs. You can buy each type multiple times. A pass is active for a period of pp consecutive days, starting on a day you choose. The pass covers the first dd travel days within that period, and those days do not have to be consecutive. An active pass cannot be paused. A travel day counts toward the day count of every active pass, even if you pay the individual fare for that day.

Figure I.1 shows two pass types offered by a webshop. The first can be activated for a period of 55 days and covers 33 days within that period. The second has a period of 3030 days and covers 55 days within that period. Buying passes is clearly cheaper than paying the individual fares for all 44 travel days. The cheapest choice is two passes of the first type, which costs less than one pass of the second type.

Input

The first line contains two integers nn and kk (1≤n≤10 0001\leq n\leq 10\,000, 0≤k≤1000\leq k\leq 100), the number of planned travel days and the number of pass types.

Each of the next nn lines contains two integers tt and ff (0≤t≤1060\leq t \leq 10^6, 1≤f≤1051\leq f\leq 10^5), the travel day and the individual fare for that day. The nn travel days are distinct and given in increasing order.

Each of the next kk lines contains three integers pp, dd, and cc (1≤p≤1061\leq p\leq 10^6, 1≤d≤p1\leq d\leq p, 1≤c≤1051\leq c\leq 10^5), describing a pass type that is valid for a period of pp days, covers the first dd travel days in that period, and costs cc.

Output

Output the minimum amount you need to spend to cover all your planned travels.

Examples5

  1. Example 1

    Input
    2 1
    0 10
    1 10
    2 2 15
    
    Expected output
    15
    
  2. Example 2

    Input
    2 1
    0 10
    2 10
    2 2 15
    
    Expected output
    20
    
  3. Example 3

    Input
    3 1
    0 10
    1 10
    2 10
    5 2 15
    
    Expected output
    25
    
  4. Example 4

    Input
    4 2
    3 80
    5 90
    24 70
    26 60
    5 3 100
    30 5 212
    
    Expected output
    200
    
  5. Example 5

    Input
    4 1
    42 9
    43 2
    44 9
    45 9
    4 3 20
    
    Expected output
    29