Buying Notebooks
InterviewTime limit1sMemory limit128 MB
Each store has a fixed shipping fee, a per-notebook price, and limited stock; buy exactly N notebooks across stores at minimum total cost.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Greedy, Array
- Solved
- No attempts yet
Problem
Minhyuk wants to buy notebooks. He has already surveyed the notebook prices at online stores.
Store sells one notebook for won and has notebooks in stock. Placing an order with a store incurs a shipping fee of won, charged only once no matter how many notebooks you buy. You may not order more than the notebooks a store has in stock.
Write a program that computes the minimum cost of buying notebooks.
Input
The first line contains the number of test cases (). Each test case has the following format.
- The first line contains the number of notebooks to buy and the number of stores (, , ).
- Each of the next lines contains a store's stock , price , and shipping fee (, ).
Output
For each test case, print the minimum cost of buying notebooks on its own line.