This page is still under construction.

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

Huffman's Greed

Time limit1sMemory limit128 MB

Summary
We build the optimal binary search tree for weighted key and gap frequencies, minimizing weighted comparison counts.
Level

Medium7 of 10

Topics
Dynamic programming, Tree, Intervals, Prefix sum
Solved
No attempts yet

Problem

We first fix some terminology on trees. A tree is defined inductively: it has a root which is either an external node (a leaf) or an internal node that has a sequence of trees as its children. An internal node is called the parent of the roots of its child trees. The level of a node is defined inductively: the root has level 00, and every other node has level one greater than its parent.

In a binary tree every internal node has exactly two children: its left sub-tree and its right sub-tree. In a labelled binary tree every internal node also carries a string, its label. A binary search tree is a labelled binary tree in which every internal node tt satisfies: all labels in the left sub-tree of tt are less than the label of tt, which in turn is less than all labels in the right sub-tree of tt. Comparisons use lexicographic (alphabetic) order on strings.

An inorder traversal visits a leaf directly, and for an internal node it first traverses the left sub-tree, then visits the node, then traverses the right sub-tree. Hence an inorder traversal of a binary search tree lists the labels in lexicographic order. Binary search trees of different shapes may still yield the same inorder sequence.

To look up a string ss, we compare ss with the label ll of the current root: we stop if s=ls = l; if s<ls < l we continue in the left sub-tree; if s>ls > l we continue in the right sub-tree. Reaching a leaf means ss is not stored in the tree.

The number of comparisons depends on ss and on the shape of the tree, so we want to build a tree that stores a given set of strings while allowing access that is as fast as possible. Since we do not know in advance which strings will be searched for, we assume a probability distribution.

Let nn be the number of strings to store, and let K1,…,KnK_1, \dots, K_n be these strings in lexicographic order. Let p1,…,pnp_1, \dots, p_n and q0,…,qnq_0, \dots, q_n be 2n+12n+1 non-negative real numbers with

∑i=1npi+∑i=0nqi=1.\sum_{i=1}^{n} p_i + \sum_{i=0}^{n} q_i = 1.

Their meaning is:

  • pip_i = probability that the search string ss equals KiK_i.
  • qiq_i = probability that ss lies strictly (lexicographically) between KiK_i and Ki+1K_{i+1}.

By convention q0q_0 is the probability that s<K1s < K_1, and qnq_n the probability that s>Kns > K_n.

We want a binary search tree over the labels K1,…,KnK_1, \dots, K_n that minimises the expected number of comparisons, namely

cost=∑i=1npi⋅(1+level⁡(Ki))+∑i=0nqi⋅level⁡(ℓi),\mathrm{cost} = \sum_{i=1}^{n} p_i \cdot \big(1 + \operatorname{level}(K_i)\big) + \sum_{i=0}^{n} q_i \cdot \operatorname{level}(\ell_i),

where ℓi\ell_i is the leaf reached when searching for a string that lies strictly between KiK_i and Ki+1K_{i+1} (using the border convention above).

For example, when n=2n = 2 there are exactly two possible binary search tree shapes; the answer is the one whose expected cost is smaller.

Input

The input contains several test cases. Each test case starts with an integer nn with 1≤n≤2001 \le n \le 200. Then follow 2n+12n+1 non-negative integers, the frequencies. Let ss be the sum of all these frequencies; you may assume 1≤s≤1 000 0001 \le s \le 1\,000\,000. The probabilities p1,…,pnp_1, \dots, p_n and q0,…,qnq_0, \dots, q_n are obtained, in this order, by dividing the frequencies by ss: the first nn frequencies give p1,…,pnp_1, \dots, p_n, and the next n+1n+1 give q0,…,qnq_0, \dots, q_n. The last test case is followed by a single 00.

Output

For each test case, output on its own line the integer cost⋅s\mathrm{cost} \cdot s, where cost\mathrm{cost} is the minimum expected number of comparisons achievable by any binary search tree for the given frequencies.

Examples2

  1. Example 1

    Input
    2
    20 15 15 25 25
    35
    142 35 58 5 20 5 10 9 15 23 129 4 52 5 38 18 9 7 2 4 266 93 5 18 18 27 5 10 11 180 4 32 21 3 21
    0 55 27 36 85 31 58 3 334 0 98 27 113 89 180 0 62 12 0 37 0 3 64 70 0 277 0 0 0 170 0 18 76 27 3 29
    0
    
    Expected output
    160
    13637
    
  2. Example 2

    Input
    1
    10 20 30
    0
    
    Expected output
    60