Plastic Bags
Time limit1sMemory limit128 MB
Pack every item weighing up to 2000 grams into capacity limited bags, including one free bag set by total price, at the lowest extra cost.
- Level
Medium7 of 10
- Topics
- Backtracking, Brute force
- Solved
- No attempts yet
Problem
A department store wants less plastic waste, so it asks shoppers to bring a cloth bag. Some customers still arrive without one, so the store gives away one free plastic bag according to how much the customer spends.
The store keeps three bag sizes, and each one holds a different weight.
- A small bag holds up to 500 grams.
- A medium bag holds up to 1250 grams.
- A large bag holds up to 2000 grams.
The total price of the purchase decides the free bag.
- Under 500 baht, the customer gets one small bag.
- From 500 baht up to but not including 1000 baht, the customer gets one medium bag.
- At 1000 baht or more, the customer gets one large bag.
No bag can carry an item heavier than 2000 grams, so the customer takes such an item home by hand. Every other item has to go into a bag. One item cannot be split between two bags, and a bag takes any number of items as long as their combined weight stays within what the bag holds.
When the single free bag is not enough, the customer buys bags. A small bag costs 5 baht, a medium bag 12 baht, and a large bag 20 baht, and the store never runs out.
Find the smallest amount the customer can pay for bags while every item weighing at most 2000 grams goes into a bag.
Input
The first line has the number of test cases ().
The first line of each test case has the number of product types the customer bought, (). Each of the next lines has three integers , , and : the price of one unit, the number of units bought, and the weight of one unit in grams (, , ).
The total price of the purchase is the sum of over all lines.
Output
For each test case print one integer on its own line: the smallest amount of baht the customer pays for bags so that every item weighing at most 2000 grams goes into a bag.