Hidden Tree

No attempts yetTime limit5sMemory limit128 MB

Problem

Consider a binary tree whose leaves carry integer weights. The tree is balanced when the weights in the left subtree of every non-leaf node sum to the same value as the weights in its right subtree. Every non-leaf node has both a left child and a right child. For example, the tree in the figure below is balanced.

Figure I.1. A balanced tree

A balanced tree is hidden in a sequence AA when the weights of all its leaves, read from left to right, form a subsequence of AA. A subsequence is what is left after deleting zero or more elements from the original sequence, keeping the order of the remaining elements.

The balanced tree in Figure I.1 is hidden in the sequence 3 4 1 3 1 2 4 4 6. Its leaves read 4 1 1 2 4 4 from left to right, and that is a subsequence of the sequence.

Given a sequence of integers, find the balanced tree hidden in it with the largest number of leaves. For the sequence above, the tree in Figure I.1 has the most leaves.

Input

The input consists of several test cases. Each test case gives one sequence AA of integers in the format

N
A1 A2 ... AN

where NN is the length of the sequence and AiA_i is its ii-th element, with 1N10001 \le N \le 1000 and 1Ai5001 \le A_i \le 500.

A line holding a single zero ends the input. The number of test cases is at most 50.

Output

For each test case, find the balanced tree hidden in AA with the largest number of leaves and print the number of its leaves in one line.