Problem Solving
Time limit1sMemory limit128 MB
Given a monthly budget M and per-problem advance and completion payments, find the minimum number of months to solve all P problems in order, where each month spends at most the previous month's budget.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
The cows have problems to solve (). They have quit producing milk and taken regular jobs like everyone else, and in a normal month they earn money ().
Their problems are so complex that they must hire consultants to solve them. Consultants are competent: a consultant can solve any single problem in one month. Each consultant demands two payments — one in advance, paid at the start of the month in which work on the problem begins, and one afterward, paid at the start of the month after the problem is solved.
Each month the cows may pay consultants only with the money earned during the previous month. The cows are spendthrifts: they can never carry money over to the next month, and any money not spent is wasted on cow candy. Thus the amount available to spend each month is exactly , and in the first month there is no previous month, so no money is available.
Because the problems depend on one another, they must be solved essentially in order. For example, problem 3 must be solved before problem 4, or at the latest during the same month as problem 4. In other words, the problems solved in any single month always form a contiguous range of problem numbers.
Determine the minimum number of months needed to solve every problem and pay every consultant in full.
Input
- Line 1: Two space-separated integers, and .
- Lines 2 through : Line describes problem with two space-separated integers and . is the advance payment made before the problem is solved and is the payment made after the problem is solved, with and .
Output
- Line 1: The minimum number of months needed to solve every problem and pay for the solutions.
Hint
The table below traces the process for the example input. The money available each month is , and no money is available in the first month.
+-------+-------+--------+---------+---------+--------+
| | Avail | Probs | Before | After | Candy |
| Month | Money | Solved | Payment | Payment | Money |
+-------+-------+--------+---------+---------+--------+
| 1 | 0 | -none- | 0 | 0 | 0 |
| 2 | 100 | 1, 2 | 40+60 | 0 | 0 |
| 3 | 100 | 3, 4 | 30+30 | 20+20 | 0 |
| 4 | 100 | -none- | 0 | 50+50 | 0 |
| 5 | 100 | 5 | 40 | 0 | 60 |
| 6 | 100 | -none- | 0 | 40 | 60 |
+-------+-------+--------+---------+---------+--------+
So solving all problems and paying for them takes months.