Life in Wartime

Time limit2sMemory limit512 MB

Summary
Given N distinct cities (all below 250) in an infinite binary heap tree, count cities that either host a unit or lie on the unique path between two units.
Level

Medium6 of 10

Topics
Tree, Hash map, Dynamic programming, Implementation
Solved
No attempts yet

Problem

War has broken out in Seokhwan. Seokhwan is a nation shaped like an enormous binary tree, made up of 1010010^{100} cities numbered 1,2,…,101001, 2, \ldots, 10^{100}. It has 10100−110^{100}-1 roads, and for 1≤i<101001 \le i < 10^{100}, the ii-th road connects city ⌊i+12⌋\lfloor \frac{i+1}{2} \rfloor to city i+1i+1. A picture of this is shown below.

Prime Minister Winston Agiseokhwan has taken on the grave task of saving Seokhwan from its crisis. The hostile nations are bent on disrupting Seokhwan's important military facilities, so to protect the people of Seokhwan it is effective to defend first the cities that armies travel through often. In Seokhwan there are NN military units stationed in distinct cities, and the units move between cities to exchange supplies and information.

A city is dangerous if a military unit is stationed there, or if there exist two distinct military units whose path passes through that city. Note that Seokhwan is a tree and a path is defined never to visit the same city twice, so the path between any two military units is always unique.

For Prime Minister Agiseokhwan, compute the number of dangerous cities in Seokhwan.

Input

The first line gives the number of military units NN. (2≤N≤250,0002 \le N \le 250{,}000)

The next NN lines give the sequence A1,…,ANA_1, \ldots, A_N of the numbers of the cities containing military units. The given cities are all distinct. (1≤Ai<2501 \le A_i < 250)

Output

Print the number of dangerous cities in Seokhwan.

Examples2

  1. Example 1

    Input
    4
    4 5 6 7
    
    Expected output
    7
    
  2. Example 2

    Input
    2
    1 4294967296
    
    Expected output
    33