Interrail Pass
InterviewTime limit1sMemory limit1024 MB
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 travel days, which are not necessarily consecutive. Each day has its own individual fare. Buying some interrail passes may save money.
There are types of interrail passes with different costs. You can buy each type multiple times. A pass is active for a period of consecutive days, starting on a day you choose. The pass covers the first 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 days and covers days within that period. The second has a period of days and covers days within that period. Buying passes is clearly cheaper than paying the individual fares for all 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 and (, ), the number of planned travel days and the number of pass types.
Each of the next lines contains two integers and (, ), the travel day and the individual fare for that day. The travel days are distinct and given in increasing order.
Each of the next lines contains three integers , , and (, , ), describing a pass type that is valid for a period of days, covers the first travel days in that period, and costs .
Output
Output the minimum amount you need to spend to cover all your planned travels.