Milk Candy
Time limit2sMemory limit512 MB
Given hints that reveal range sums with costs, and NPCs that force exactly k_i purchases each, find the minimum total cost to determine all n values, or report -1.
- Level
Hard9 of 10
- Topics
- Matrix, Combinatorics, Greedy, Bit manipulation
- Solved
- No attempts yet
Problem
Calabash is playing an RPG on his computer. There are unknown numbers and NPCs who sell hints. The -th NPC sells hints. Each hint has three integers , , and . Buying it costs coins and reveals the value of .
The goal is to determine all unknown numbers. Calabash buys hints optimally, but each NPC is greedy: Calabash must buy exactly hints from the -th NPC. A single hint cannot be bought more than once.
Find the minimum number of coins Calabash must pay to determine all numbers, or determine that it is impossible.
Input
The first line contains (), the number of test cases.
Each test case starts with and (). Then, for each NPC, there is a line with and (), followed by lines, each containing , , and (, ).
The sum of all in a test case is at most 80.
Output
For each test case, print the minimum number of coins on a single line. If there is no solution, print .