#P4539. [SCOI2006] zh_tree

[SCOI2006] zh_tree

Description

Teacher Zhang designed a special binary search tree for his work.

He named this binary tree zh_tree. For a zh_tree with nn nodes, its inorder traversal is exactly (1,2,3,,n)(1, 2, 3, \cdots, n), where the numbers 1,2,3,,n1, 2, 3, \cdots, n are the identifiers of the nodes. The nn nodes correspond to nn distinct words that appear in a set of academic papers.

Let the jj-th word appear djd_j times; for example, d2=10d_2 = 10 means that the word corresponding to node 22 appears 1010 times. Let SS be the total number of word occurrences in this set of papers. Obviously, S=d1+d2++dnS = d_1 + d_2 + \cdots + d_n. Define fj=djSf_j = \frac{d_j}{S} as the probability (frequency) of the jj-th word.

The root node has depth 00. If node jj has depth rr, then its access cost hjh_j is hj=k(r+1)+ch_j = k (r + 1) + c, where kk and cc are known positive constants not exceeding 100100.

A zh_tree is a binary tree that minimizes h1f1+h2f2++hnfnh_1 f_1 + h_2 f_2 + \cdots + h_n f_n.

We call the expression above the average access cost of the zh_tree. Please design a zh_tree based on the given data.

Input Format

Line 1: Three positive numbers separated by spaces: n,k,cn, k, c, where n<30n < 30 is an integer. kk and cc are positive real numbers not exceeding 100100.

Line 2: nn positive integers separated by spaces, the occurrence counts for each word (each count <200< 200).

Output Format

Output one positive real number on a single line, rounded to 33 decimal places: the minimal average access cost of the zh_tree.

4 2 3.5
20 30 50 20
7.000

Hint

The original problem also required constructing the tree and outputting its inorder traversal. For various reasons, we removed that second requirement to keep compatibility with existing solutions and code.

Translated by ChatGPT 5