Pearls
InterviewTime limit1sMemory limit128 MB
Given demand and price per pearl for classes in increasing quality order, find the cheapest way to buy all pearls when each class's order may be upgraded to a higher class, paying 10 extra pearls' worth per purchase.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
In Pearlania, everybody is fond of pearls. A company called The Royal Pearl produces a great deal of pearl jewelry. It delivers to the royal family, but it also makes bracelets and necklaces for ordinary people, using pearls of much lower quality.
Pearls are separated into 100 quality classes. Each class is identified by the price of a single pearl in that class; this price is unique to the class. A higher-quality class always costs more per pearl than a lower one.
Every month the stock manager prepares a list of how many pearls are needed in each quality class. The pearls are bought on the local pearl market. In addition to the per-pearl price, every completed purchase in a class costs an extra amount equal to the price of ten pearls of that class (this surcharge discourages buying just a single pearl). So buying pearls in a class whose price is costs .
To save money, the CFO may buy pearls in a higher quality class than requested, but never in a lower one. Customers do not mind receiving better pearls as long as the price stays the same. Merging several classes into one higher-class purchase can reduce the total surcharge.
For example, suppose 5 pearls are needed in the 10-Euro class and 100 pearls in the 20-Euro class. Buying them separately costs Euro. Buying all 105 pearls in the 20-Euro class costs only Euro.
Given the number of pearls needed and the price per pearl for several quality classes, compute the lowest possible total price to buy everything on the list. Pearls may be bought in the requested class or in any higher class, but never in a lower one.
Input
The first line contains the number of test cases.
Each test case begins with a line containing the number of quality classes (). Then follow lines, each with two integers and : the number of pearls needed in that class () and the price per pearl in that class (). The classes are listed in ascending order of quality, and therefore of price. All numbers in the input are integers.
Output
For each test case, output a single line containing one integer: the lowest possible total price needed to buy everything on the list.