Street Tree Props (Large)
Time limit5sMemory limit512 MB
Assign one or two sticks to each of N trees so every tree reaches support B while the total used force is as small as possible.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Two pointers
- Solved
- No attempts yet
Problem
You were elected mayor of city G. As part of a beautification program you decide to plant street trees along the roads. Only after buying the trees do you learn that a young tree needs props to grow straight.
You want to know whether the wooden sticks you already own can prop up every tree you bought. The rules are these.
- Every tree has to be propped up. None may be left out.
- Each tree needs the same supporting force .
- To prop a tree you stand a stick against it, and that stick's supporting force has to be at least .
- Sticks with the same supporting force are grouped into one kind, and you know how many sticks each kind holds.
- For the sake of appearance a tree takes at most two props. Two sticks hold the tree with the sum of their supporting forces, so that sum has to be at least .
- One stick props at most one tree, and a stick may be left unused.
- Among the ways that satisfy every rule above, minimize the total supporting force of the sticks you use.
Input
The variables are defined as follows.
- = number of test cases
- = number of trees
- = supporting force one tree needs (the same for every tree)
- = number of stick kinds
- = supporting force of one stick of the -th kind
- = number of sticks of the -th kind
The input has this format.
T
N M B
p1 q1
p2 q2
...
pM qM
The first line holds . Then test cases follow. The first line of a test case holds , and separated by spaces, and each of the next lines holds and .
Limits
- Every input value is an integer.
Output
For each test case print one line in the form Case #x: y, where is the test case number starting from 1. If every street tree can be propped up, is the smallest possible total supporting force of the sticks used. Otherwise is -1.