Tree paths XOR


Submit solution

Points: 100
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, BrainF***, C, C#, C++, Dart, Go, Java, JS, Kotlin, Lua, Pascal, Perl, Prolog, Python, Scala, Swift, VB

About thirty years ago, young Carl 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 Carl was irritated by one sentence. Namely, one of the speakers claimed he was more appreciative of the logical operation AND than the logical operation OR because, regardless of the winner’s identity, to him both Alice and Bob were winners of the national competition, instead of the winner being Alice or Bob. Carl went mad, got up and started explaining to the audience that this is an operation known as exclusive OR (popular XOR). After having given his lecture, he gave the distinguished speaker the next task to verify his understanding.

There is a given tree consisting of N nodes, where each node is assigned a value. The value of a path on that tree is defined as the exclusive OR 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, Carl has finally persuaded the authors of the DMOJ tasks to include this task in one of the rounds. Help us return Carl’s faith in the future of competitive programming.

Note: Exclusive OR (\oplus) 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 1 if and only if that bit in exactly one operand is set to 1.

Input

The first line contains a positive integer N (1 \le N \le 100\,000) that denotes the number of nodes in the tree.

The second line contains N integers v_i (0 \le v_i \le 3\,000\,000) separated by space, i^{th} value representing the value of the i^{th} node.

The following (N-1) lines contain two numbers a_j and b_j (1 \le a_j , b_j \le N ) that indicate that there is an edge between the nodes a_j and b_j.

Output

Print the required sum of values for all tree paths.

Samples

Input 1
3
1 2 3
1 2
2 3
Output 1
10
Input 2
5
2 3 4 2 1
1 2
1 3
3 4
3 5
Output 2
64
Input 3
6
5 4 1 3 3 3
3 1
3 5
4 3
4 2
2 6
Output 3
85
Clarification of the first sample test:
  • The value of the path from node 1 to node 1 is 1
  • The value of the path from node 1 to node 2 is 1 \oplus 2 = 3
  • The value of the path from node 1 to node 3 is 1 \oplus 2 \oplus 3 = 0
  • The value of the path from node 2 to node 2 is 2
  • The value of the path from node 2 to node 3 is 2 \oplus 3 = 1
  • The value of the path from node 3 to node 3 is 3

The sum of all paths is 1 + 3 + 0 + 2 + 1 + 3 = 10.


Comments

There are no comments at the moment.