Decisions, Decisions

Given the truth table of an n-variable boolean function, count the vertices in its unique minimal binary decision diagram.

Medium6Dynamic programmingDivide and conquerTreeRecursionNo attempts yetTime limit2sMemory limit512 MB

Problem

Let x0,,xn1x_0, \dots, x_{n-1} be nn boolean variables, that is, variables taking only the values 0 and 1. A binary decision diagram (BDD) over these variables is a diagrammatic representation of a boolean function f(x0,,xn1)f(x_0, \dots, x_{n-1}).

A BDD is a rooted binary tree in which every internal vertex has exactly two children. The two edges joining an internal vertex vv to its children are labelled 0 and 1, one each. Every leaf vertex is labelled 0 or 1. A BDD may consist of a single vertex, and that vertex is then both the root and a leaf.

Given an input (x0,,xn1)(x_0, \dots, x_{n-1}), the boolean function represented by the BDD is evaluated as follows.

  • let vv be the root vertex
  • let i0i \leftarrow 0
  • while vv is not a leaf do
    • replace vv with the child of vv reached by the edge labelled xix_i
    • increase ii by 1
  • output the label of the leaf vertex vv

Consider the function f(x0,x1,x2)f(x_0, x_1, x_2) represented by the BDD in the picture. To evaluate f(1,0,1)f(1, 0, 1) you start at the root and descend along the edges labelled 1, 0, and then 1. You reach a leaf labelled 1, so f(1,0,1)=1f(1, 0, 1) = 1.

A BDD is minimal if there is no way to replace the subtree of an internal vertex by a single leaf and obtain a BDD defining the same boolean function. The BDD in the picture is minimal. For every boolean function the minimal BDD representing it is unique.

In this problem an nn-variable boolean function is given as the list of the 2n2^n values it takes. Compute the number of vertices of the minimal BDD representing this function.

Input

The first line contains one integer nn (1n181 \le n \le 18). The second line contains 2n2^n values describing an nn-variable boolean function. Each value is 0 or 1.

The values are indexed from 0 to 2n12^n - 1. The ii-th value is f(x0,,xn1)f(x_0, \dots, x_{n-1}), where xjx_j is the coefficient of 2j2^j in the binary expansion of ii, that is, the jj-th least significant bit of ii.

The third sample input corresponds to the BDD in the picture.

Output

Print one integer mm, the number of vertices of the unique minimal BDD representing the boolean function from the input.