#ds1003. Strange Memory
Strange Memory
Strange Memory
Problem Source
CCPC Changchun 2020 F
Problem Description
Once there was a rooted tree. The tree contained nodes, which were numbered . The node numbered was the root of the tree. Besides, every node was assigned a number . You were surprised to find that there were several pairs of nodes satisfying
where denotes the bitwise XOR operation, and is the lowest common ancestor of and , or formally, the lowest (i.e. deepest) node that has both and as descendants.
Unfortunately, you cannot remember all such pairs, and only remember the sum of for all different pairs of nodes satisfying the above property. Note that and are considered the same here. In other words, you will only be able to recall
$$\sum_{i=1}^n \sum_{j=i+1}^n \left[ a_i \oplus a_j = a_{\mathrm{lca}(i, j)} \right](i \oplus j).$$You are asked to calculate it now in order to memorize it better in the future.
Input Format
The first line contains a single integer ().
The second line contains integers ().
Each of the next lines contains integers and (), indicating that there is an edge between and . It is guaranteed that these edges form a tree.
Output Format
Print what you will memorize in the future.
Sample Input
6
4 2 1 6 6 5
1 2
2 3
1 4
4 5
4 6
Sample Output
18