- محدودیت زمان: ۱ ثانیه
- محدودیت حافظه: ۲۵۶ مگابایت
Jeff and Sehri decided to divide Semnan among themselves.
for simplicity let's assume Semnan is a tree consisting of n nodes.
A tree with n nodes is a undirected connected graph with with n−1 edges.
We also define an array F of length N of integers between 1 to 100,000.
Amin gives each of Semnan's tree nodes to either Jeff and Sehri, after Amin divides Semnan each of Jeff or Sehri will have a forest, where forest is a disjoint union of trees.
if Jeff's forests consists of k connected components and each of them have c1, c2, ..., ck number of nodes, his happiness is
i=1∑kFCi
Sehri's happiness is calculated in the same way.
and after that, the amount of happiness of Amin will be the XOR of happiness of Jeff and Sehri.
Amin wonders what is the sum of his happiness over all 2n possible divisons of Semnan?
Help Amin with this problem.
the first line of input consists of the number n the number of nodes in Semnan.
the next line will be n−1 numbers p2, p3, ..., pn where pi is an edge between node number i and pi.
then in the next line there are N numbers denoting array F where the i-th number is Fi.
1≤n≤26
1≤pi≤i+1
1≤Fi≤100,000
output🔗
output should consist of a single number the sum of Amin's happiness over all possible 2n divisions of Semnan.
example🔗
sample output 1🔗
- If Jeff gets no nodes Jeff's happiness would be zero but Sehri would get all 3 nodes then Sehri's forest would consist of one component of size 3 then Sehri's happiness would be equal to F3=15 since 15xor0=15 Amin's happiness would be equal to 15.
- If Jeff gets one node, Jeff's forest would consist of one connected component of size 1 so Jeff's happiness would be equal to F1=10 but if the node Jeff gets is node number 1 Sehri's forest would consist of 2 connected components of size one so Sehri's happiness would be F1+F1=20 and Amin's happiness would be 10xor20=30 but if Jeff gets is the node 2 or 3 Sehri's forest would consist of one connected component of size 2 so Sehri's happiness would be F2=5 and Amin's happiness would be equal to 10xor5=15.
- If Jeff gets two or three nodes it is similar to the previous cases so we have 15+30+15+15+30+15+15+15=150
sample output 2🔗