Street Tree Props (Large)

Time limit5sMemory limit512 MB

Summary
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 BB.
  • To prop a tree you stand a stick against it, and that stick's supporting force has to be at least BB.
  • 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 BB.
  • 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.

  • TT = number of test cases
  • NN = number of trees
  • BB = supporting force one tree needs (the same for every tree)
  • MM = number of stick kinds
  • pip_i = supporting force of one stick of the ii-th kind
  • qiq_i = number of sticks of the ii-th kind

The input has this format.

T
N M B
p1 q1
p2 q2
...
pM qM

The first line holds TT. Then TT test cases follow. The first line of a test case holds NN, MM and BB separated by spaces, and each of the next MM lines holds pip_i and qiq_i.

Limits

  • Every input value is an integer.
  • 1≤T≤501 \le T \le 50
  • 1≤N≤1000001 \le N \le 100000
  • 1≤M≤10001 \le M \le 1000
  • 1≤B≤100001 \le B \le 10000
  • 1≤pi≤200001 \le p_i \le 20000
  • 1≤qi≤2000001 \le q_i \le 200000

Output

For each test case print one line in the form Case #x: y, where xx is the test case number starting from 1. If every street tree can be propped up, yy is the smallest possible total supporting force of the sticks used. Otherwise yy is -1.

Examples1

  1. Example 1

    Input
    2
    2 3 10
    6 1
    4 1
    12 2
    2 3 10
    3 1
    5 1
    10 1
    
    Expected output
    Case #1: 22
    Case #2: -1