#P17877. 糖葫芦 / tanghulu
糖葫芦 / tanghulu
Background
Tanghulu (candied hawthorn skewers) is one of the most representative traditional snacks of old Beijing. Sweet, sour and glistening, it is often seen at the lively winter temple fairs and in the hutong alleys.
The key to making authentic Beijing tanghulu lies in "boiling the sugar" and "coating with sugar". First, wash and pit the hawthorns and skewer them on bamboo sticks; then boil white sugar with water until it bubbles, thickens, and turns a clear caramel color. When the syrup is just right, quickly dip the whole skewer into the hot, thick syrup and give it a light turn so that it is evenly coated with a thin layer of sugar; then take it out and let it cool, forming a thin, crisp and translucent sugar shell.
Problem Description
Little M plans to learn to make some tanghulu. He has already prepared the fruit skewers and boiled the syrup; next he needs to do the sugar-coating step.
Little M has prepared pots of syrup, where the -th () pot initially contains units of syrup. To make a tanghulu of length , he must choose a pot whose current remaining amount is at least and dip the skewer into it; after it is made, the remaining amount of that pot decreases by unit (if the remaining amount drops to , that pot is removed).
Little M needs to make batches of tanghulu in increasing order of batch number. The -th () batch has skewers, each of length . The tanghulu must be made one by one, and the -th batch can only start after all tanghulu of the -th batch are finished. When making each skewer, Little M can independently choose from all current pots of syrup.
Before starting, and before or after making any skewer, Little M may perform any number of merge operations. Merging has a given loss constant : each merge always loses units of syrup. In each merge, Little M chooses two pots with current remaining amounts satisfying , merges them into a new pot with remaining amount , and removes the two original pots.
Since merging causes loss, Little M wants to merge as few times as possible. Find the minimum number of merges needed to finish making all the tanghulu, or report that it is impossible to finish no matter what.
Input Format
This problem contains multiple test cases.
The first line of the input contains two integers , the testcase number and the number of test cases, respectively. In particular, if , this testcase is a sample.
Then test cases follow. For each test case:
- The first line contains three positive integers , the initial number of syrup pots, the number of batches of tanghulu, and the amount of syrup lost in each merge, respectively.
- The second line contains positive integers , the initial amounts of the syrup pots.
- Each of the next lines, the -th line, contains two positive integers , the minimum remaining amount of syrup required to make each skewer in the -th batch, and the number of skewers in this batch, respectively.
Output Format
For each test case, output one line containing a single integer, the minimum number of merges needed. If it is impossible to finish all the making, output .
0 3
5 3 2
2 3 3 5 8
4 2
7 2
8 2
1 1 1
3
2 3
2 2 2
3 3
3 2
2 2
3
-1
0
Hint
【Sample 1 Explanation】
For the first test case, one valid plan is as follows:
- In the first batch, use the pot with initial amount to make two skewers; its remaining amount becomes .
- In the second batch, use the pot with initial amount to make two skewers; its remaining amount becomes .
At this point, the remaining amounts of the pots are .
For the third batch, operate in the following order:
- Merge the two pots with remaining amounts , getting units of syrup.
- Merge the two pots with remaining amounts , getting units of syrup.
- Use this pot to make one skewer; its remaining amount becomes .
- Merge the two pots with remaining amounts , getting units of syrup.
- Use this pot to make the last skewer.
There are merges in total. It can be proved that no valid plan uses fewer than merges.
For the second test case, after making two skewers, the only pot of syrup has remaining amount , so the third skewer cannot be made, and no merge is possible either.
For the third test case, in the first batch use two pots to make one skewer each; after that, both pots have units of syrup left. In the second batch, use the two pots to make one skewer each, and all the making is finished without any merge.
【Sample 2】
See tanghulu/tanghulu2.in and tanghulu/tanghulu2.ans in the problem directory.
This sample satisfies the constraints of testcases .
【Sample 3】
See tanghulu/tanghulu3.in and tanghulu/tanghulu3.ans in the problem directory.
This sample satisfies the constraints of testcases .
【Sample 4】
See tanghulu/tanghulu4.in and tanghulu/tanghulu4.ans in the problem directory.
This sample satisfies the constraints of testcases .
【Sample 5】
See tanghulu/tanghulu5.in and tanghulu/tanghulu5.ans in the problem directory.
This sample satisfies the constraints of testcases .
【Sample 6】
See tanghulu/tanghulu6.in and tanghulu/tanghulu6.ans in the problem directory.
This sample satisfies the constraints of testcases .
【Sample 7】
See tanghulu/tanghulu7.in and tanghulu/tanghulu7.ans in the problem directory.
This sample satisfies the constraints of testcase .
【Constraints】
Let be the sum of over all test cases within a single testcase, and be the sum of over all test cases within a single testcase. For all testdata:
- , ;
- , ;
- ;
- For all , ;
- For all , .
The specific constraints of each testcase are shown in the table below.
::cute-table{tuack} |Testcase No.|||||Special Property| |:----:|:----:|:----:|:----:|:----:|:----:| ||||||A| ||||||None| ||||^||B| ||^|^|||None| ||^|^|||^| ||^|^|||^| ||^|^|||A| ||^|^|^|^|None| ||^||^||^|
Special Property A: For all , .
Special Property B: For all , .
Translated by QwenWork