This page is still under construction.

Parts of this page are still being built. What you see may change.

Multi-Element Binary Search Tree

Time limit1sMemory limit1024 MB

Summary
Given sorted search probabilities and level-dependent node capacities, find the minimum expected number of comparisons for a multi-element BST.
Level

Medium7 of 10

Topics
Dynamic programming, Tree, Binary search
Solved
No attempts yet

Problem

A multi-element binary search tree differs from an ordinary binary search tree in that each node may hold several elements. The core binary-search-tree condition still holds in a generalized form: for any node, every element in that node's left subtree must be smaller, and every element in its right subtree larger, than every element the node itself contains.

Therefore an element can be looked up in a multi-element binary search tree in exactly the same way as in an ordinary one. At each node we decide whether the sought element must be in this node, or whether to descend into the left or the right subtree. Counting each such decision as one operation, the number of operations needed to reach the node that holds a given element equals exactly the number of levels from the root down to that node (the root is at level 11).

Because of how node memory is managed, the maximum number of elements a single node may hold differs from level to level. With the root at level 11, the maximum number of elements mim_i in a node at level ii is defined, for given integers MM, KK, and DjD_j, by

mi={M(i=1)max⁡(1, mi−1−D((i−2) mod K)+1)(i>1)m_i = \begin{cases} M & (i = 1) \\ \max\bigl(1,\ m_{i-1} - D_{((i-2) \bmod K) + 1}\bigr) & (i > 1) \end{cases}

That is, the value DjD_j subtracted when descending one level cycles as D1D_1 at level 22, D2D_2 at level 33, …\ldots, DKD_K at level K+1K+1, then D1D_1 again at level K+2K+2, and so on. For example, if M=4M = 4, K=2K = 2, D1=1D_1 = 1, D2=2D_2 = 2, then the root may hold at most m1=M=4m_1 = M = 4 elements, each of its direct children at most m2=m1−D1=4−1=3m_2 = m_1 - D_1 = 4 - 1 = 3, each of their children m3=m2−D2=3−2=1m_3 = m_2 - D_2 = 3 - 2 = 1, and every deeper node at most mi=1m_i = 1 element.

Moreover, different elements are searched for with different probabilities, so a balanced tree does not necessarily minimize the average lookup time. For instance, if the smallest element is searched for far more often than any other, it is advantageous to place it at the root, in which case the entire left subtree stays empty.

For the given set of elements and their search probabilities, find the minimum average number of operations per lookup over all optimally shaped multi-element binary search trees.

Input

The first line contains three integers NN, MM, and KK, where NN is the number of elements (1≤N≤1001 \le N \le 100), 1≤M≤N1 \le M \le N, and 1≤K≤N1 \le K \le N. Each of the next KK lines contains one integer: the value on line j+1j+1 is DjD_j (0≤Dj≤M0 \le D_j \le M). The last line contains NN real numbers PiP_i (0≤Pi≤10 \le P_i \le 1 and ∑i=1NPi=1\sum_{i=1}^{N} P_i = 1), the search probability of each element. The probabilities are listed in order of element value (the first probability belongs to the smallest element). The actual element values do not matter for the solution; you may assume they are all distinct.

Output

Print, on a single line, the minimum average number of operations per lookup in an optimally shaped tree, rounded to exactly 77 digits after the decimal point.

Note

The optimal tree shape for the second test case is shown below.

Examples2

  1. Example 1

    Input
    4 2 1
    2
    0.2 0.2 0.3 0.3
    
    Expected output
    1.5000000
    
  2. Example 2

    Input
    13 4 2
    1
    0
    0.06 0.06 0.06 0.06 0.06 0.06 0.115 0.115 0.115 0.115 0.06 0.06 0.06
    
    Expected output
    1.7200000