CF BUDDY
← Problems·

1070C · Cloud Computing

2000 · data structures, greedy

Problem: Buber is a Berland technology company that specializes in waste of investor's money. Recently Buber decided to transfer its infrastructure to a cloud. The company decided to rent CPU cores in the cloud for nn consecutive days, which are numbered from 11 to nn. Buber requires kk CPU cores each day.

The cloud provider offers mm tariff plans, the ii-th tariff plan is characterized by the following parameters:

  • lil_i and rir_i — the ii-th tariff plan is available only on days from lil_i to rir_i, inclusive,
  • cic_i — the number of cores per day available for rent on the ii-th tariff plan,
  • pip_i — the price of renting one core per day on the ii-th tariff plan.

Buber can arbitrarily share its computing core needs between the tariff plans. Every day Buber can rent an arbitrary number of cores (from 0 to cic_i) on each of the available plans. The number of rented cores on a tariff plan can vary arbitrarily from day to day.

Find the minimum amount of money that Buber will pay for its work for nn days from 11 to nn. If on a day the total number of cores for all available tariff plans is strictly less than kk, then this day Buber will have to work on fewer cores (and it rents all the available cores), otherwise Buber rents exactly kk cores this day.

Input Format: The first line of the input contains three integers nn, kk and mm (1n,k106,1m21051 \le n,k \le 10^6, 1 \le m \le 2\cdot10^5) — the number of days to analyze, the desired daily number of cores, the number of tariff plans.

The following mm lines contain descriptions of tariff plans, one description per line. Each line contains four integers lil_i, rir_i, cic_i, pip_i (1lirin1 \le l_i \le r_i \le n, 1ci,pi1061 \le c_i, p_i \le 10^6), where lil_i and rir_i are starting and finishing days of the ii-th tariff plan, cic_i — number of cores, pip_i — price of a single core for daily rent on the ii-th tariff plan.

Output Format: Print a single integer number — the minimal amount of money that Buber will pay.

Sample Cases

Case 1

Input

5 7 3
1 4 5 3
1 3 5 2
2 5 10 1

Output

44

Case 2

Input

7 13 5
2 3 10 7
3 5 10 10
1 2 10 6
4 5 10 9
3 4 10 8

Output

462

Case 3

Input

4 100 3
3 3 2 5
1 1 3 2
2 4 4 4

Output

64

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback