About thirty years ago, young Krešo participated for the first time in the national informatics
competition. Similar to today, the opening of the competition consisted of a series of speakers who
tried to demonstrate the importance of this event to the contestants through motivational messages.
The audience, with enthusiasm, began applauding every couple of seconds, but Krešo was irritated
by one sentence. Namely, one of the speakers claimed he was more appreciative of the logical
operation than the logical operation
because, regardless of the winner's identity, to him both
Mirko and Slavko were winners of the national competition, instead of the winner being Mirko or
Slavko. Krešo went mad, got up and started explaining to the audience that this is an operation
known as exclusive
(popular
). After having given his lecture, he gave the distinguished
speaker the next task to verify his understanding.
There is a given tree consisting of nodes, where each node is assigned a value. The value of a
path on that tree is defined as the exclusive
of all nodes' values on that path. Your task is to
determine the sum of the values of all paths of the tree, including paths containing only one node.
Thirty years later, Krešo has finally persuaded the authors of the COCI tasks to include this task in one of the rounds. Help us return Krešo's faith in the future of competitive programming.
Note: Exclusive
is a binary operation that is applied separately on each pair of corresponding
bits of its two operands so that some bit in the result is set to
if and only if that bit in exactly one
operand is set to
.
Input
The first line contains a positive integer
that denotes the number of nodes in the
tree.
The second line contains integers
separated by space,
value representing
the value of the
node.
The following lines contain two numbers
and
that indicate that there is an
edge between the nodes
and
.
Output
Print the required sum of values for all tree paths.
Scoring
In test cases worth 30% of total points, will be less than or equal to
.
In test cases worth 50% of total points, will be less than or equal to
.
In test cases worth additional 20% of total points, each node will be connected to
node
.
Sample Input 1
3
1 2 3
1 2
2 3
Sample Output 1
10
Explanation for Sample Output 1
- The value of the path from node
to node
is
- The value of the path from node
to node
is
- The value of the path from node
to node
is
- The value of the path from node
to node
is
- The value of the path from node
to node
is
- The value of the path from node
to node
is
The sum of all paths is .
Sample Input 2
5
2 3 4 2 1
1 2
1 3
3 4
3 5
Sample Output 2
64
Sample Input 3
6
5 4 1 3 3 3
3 1
3 5
4 3
4 2
2 6
Sample Output 3
85
Comments