Selling CPUs
InterviewTime limit2sMemory limit512 MB
Sell up to c CPUs across m ordered merchants, each paying p_i for exactly i CPUs in one deal, to maximize total money.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
You got a job at a CPU factory. After a hard month of labor your boss paid you with identical CPUs instead of cash, because the company is short on money right now.
You cannot live on CPUs alone, so you want to sell them at the market and buy what you need with the proceeds. The market is strict about how you may do business. You may enter the market once, you may trade with each merchant only once, and you have to visit the merchants in a fixed order. The organizers numbered the merchants from to , and you must visit them in that order. Every merchant has his own price for each quantity of CPUs.
At a merchant you can walk past without selling anything, or you can sell some of the CPUs you still hold in a single deal.
Input
The first line has two integers and (), the number of CPUs and the number of merchants.
Each of the next lines describes one merchant, in order from merchant to merchant . Such a line has integers (), where is the amount of money that merchant pays for CPUs.
Output
Print the maximum amount of money you can make by selling the CPUs you have.
You do not have to sell all of them.