This page is still under construction.

Parts of this page are still being built. What you see may change.

XOR MST

Time limit2sMemory limit512 MB

Summary
Given N labeled vertices where the edge between any two has weight equal to the XOR of their labels, find the total cost of the minimum spanning tree.
Level

Hard8 of 10

Topics
Trie, Minimum spanning tree, Divide and conquer, Bit manipulation
Solved
No attempts yet

Problem

There is an undirected graph with NN vertices. The ii-th vertex is labeled with an integer AiA_i. The weight of the edge connecting vertex ii and vertex jj is Ai⊕AjA_i \oplus A_j.

Find the cost of the minimum spanning tree (MST) of this graph.

Input

The first line gives the number of vertices NN. The second line gives A1,A2,…,ANA_1, A_2, \ldots, A_N.

Output

Print the cost of the MST on the first line.

Constraints

  • 1≤N≤200,0001 \le N \le 200{,}000
  • 0≤Ai<2300 \le A_i < 2^{30}

Examples2

  1. Example 1

    Input
    5
    1 2 3 4 5
    
    Expected output
    8
    
  2. Example 2

    Input
    4
    1 2 3 4
    
    Expected output
    8