Under construction

Still being built. Things may change

Tree Transformation Cost

Time limit3sMemory limit1024 MB

Summary
Find the minimum cost to turn one weighted ordered rooted tree into another using grow, expand, contract, and relabel operations.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Recursion
Solved
No attempts yet

Problem

You are given two weighted rooted trees. For each vertex, the children are ordered from left to right. There are four elementary operations.

  1. Growing: for a vertex xx with children y1,…,ymy_1, \ldots, y_m, add a new vertex zz and an edge between xx and zz, then insert zz at position kk of the children list, so the list becomes y1,…,yk−1,z,yk,…,ymy_1, \ldots, y_{k-1}, z, y_k, \ldots, y_m. The cost is c1c_1 times the weight of the new edge (x,z)(x, z).

  2. Expansion: for a vertex xx with children y1,…,ymy_1, \ldots, y_m, choose an interval [l,r][l, r] with 1≤l≤r≤m1 \le l \le r \le m. Add a new vertex zz as the parent of yl,…,yry_l, \ldots, y_r, and add an edge between xx and zz. The children list of xx becomes y1,…,yl−1,z,yr+1,…,ymy_1, \ldots, y_{l-1}, z, y_{r+1}, \ldots, y_m, and the children list of zz is yl,…,yry_l, \ldots, y_r. Each edge (z,yi)(z, y_i) keeps the weight of (x,yi)(x, y_i) from the original tree. The cost is c1c_1 times the weight of the new edge (x,z)(x, z).

  3. Contraction: for a vertex xx, choose a child yky_k whose children list is z1,…,zpz_1, \ldots, z_p, and contract the edge (x,yk)(x, y_k). Vertex yky_k is removed, and the children list of xx becomes y1,…,yk−1,z1,…,zp,yk+1,…,ymy_1, \ldots, y_{k-1}, z_1, \ldots, z_p, y_{k+1}, \ldots, y_m. Each edge (x,zi)(x, z_i) keeps the weight of (yk,zi)(y_k, z_i). The cost is c2c_2 times the weight of the edge (x,yk)(x, y_k) in the original tree.

  4. Relabeling: for a vertex xx and its child yy, change the weight of edge (x,y)(x, y) from w1w_1 to w2w_2. The cost is c3⋅∣w1−w2∣c_3 \cdot |w_1 - w_2|.

An edge (x,z)(x, z) added by growing or expansion cannot be relabeled. An edge that has been relabeled cannot be contracted.

You want to turn the first tree into the second tree. Output the minimum total cost. Two trees are the same if there is a bijection between their vertices that preserves the root and the order of children, and the weights of corresponding edges match.

Input

The first line contains three integers c1c_1, c2c_2, and c3c_3 (1≤c1,c2,c3≤1061 \le c_1, c_2, c_3 \le 10^6). Then the two trees follow. For each tree, the first line contains nn, the number of vertices. Each of the next nn lines starts with kk, the number of children, followed by 2k2k integers c1,w1,…,ck,wkc_1, w_1, \ldots, c_k, w_k (0≤ci≤1060 \le c_i \le 10^6), which give the children list and the weights of the edges to the children.

The first tree has at most 50 vertices, and the second tree has at most 2000 vertices.

Output

Print the answer.

Examples1

  1. Example 1

    Input
    1 1 2
    4
    2 2 5 4 2
    1 3 1
    0
    0
    3
    2 2 1 3 2
    0
    0
    
    Expected output
    5