• محدودیت زمان: ۱ ثانیه
  • محدودیت حافظه: ۲۵۶ مگابایت

Leila is a Bioinformatician, interested in studying Bacterial evolution. In one experiment on a special type of Bacteria, she started from a single bacterium, put it on a plate, and monitored the bacterial division, until she obtained a population of kk bacteria. During the process, she carefully reported the evolutionary relations between bacteria. Precisely, for each bacterium, she reported its parent bacterium.

In the next step, she extracted DNA sequences of kk bacteria in the final population, by NGS technology. Each DNA sequence is represented as a string of length mm from the alphabet set A,T,C,G{A, T, C, G}.

The NGS technology has a drawback: it produces a lot of missing values. So, there are a lot of unknown characters indicated by ? in the extracted sequences. Considering the evolutionary relationship between bacteria, Leila wants to impute the missing values. Among all possible imputations, she wants to find the minimum cost imputation from an evolutionary perspective.

The problem is defined as follows. A rooted tree TT is given, and for each leaf vv of TT, a string bvb_v of length mm from the character set A,T,C,G,?{A, T, C, G, ?} is given. A transition cost matrix Δ\Delta is also given, where Δ(x,y)(x,yA,T,C,G)\Delta(x, y) (x, y \in {A, T, C, G}) represents the cost of a transition from an xx character to a yy character, from a parent to its child.

A feasible imputation, assigns a string su of length mm from the character set A,T,C,G{A, T, C, G} to each vertex uu, where for each leaf vv of TT, svs_v is equal to bvb_v except for ? characters in bvb_v. The evolutionary cost of an imputation is defined as the sum of evolutionary costs of all edges. The evolutionary cost of an edge between parent uu and child ww, is defined as i=1mΔ(su[i],sw[i])\sum_{i=1}^{m} \Delta(s_u[i], s_w[i]), where su[i]s_u[i] is the ii-th character of sus_u.

Leila wants to find a feasible imputation for TT, which has the minimum evolutionary cost among all feasible imputations. The tree TT, transition cost matrix Δ\Delta, and a string bvb_v for each leaf vv are given. You should write a program to compute the minimum evolutionary cost of feasible imputations.

ورودی

The first line of the input contains an integer nn (2n10,0002 \leq n \leq 10, 000) denoting the number of vertices of TT. The vertices of TT are numbered from 11 to nn. The root of the tree is numbered 11. The root is never considered as a leaf, even if it has only one child. The next n1n - 1 lines describe the edges of TT; each line contains two endpoints of an edge separated by spaces. In the next four lines, the evolutionary cost matrix Δ\Delta is given; each line is for one row of Δ\Delta. Rows (corresponding to a parent) and columns (corresponding to a child) of Δ\Delta are ordered to respectively represent characters A,T,CA, T, C and GG. All entries of Δ\Delta are non-negative integers not more than 10610^6. The next line just contains kk, the number of leaves. Finally, each leaf vv (its number) and its bvb_v which is a string of size mm (1m2001 \leq m \leq 200) appear in one line.

خروجی

In one line, print the minimum evolutionary cost of feasible imputations.

مثال

ورودی نمونه ۱

3
1 2
1 3
0 3 4 4
4 0 4 4
4 4 2 4
1 1 1 0
2
2 AAC
3 T?C
Plain text

خروجی نمونه ۱

4
Plain text

ارسال پاسخ برای این سؤال
فایلی انتخاب نشده است.