This page is still under construction.

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

Words 2

Time limit1sMemory limit128 MB

Summary
Given exponents k1..kn, find the smallest m such that the concatenation of h_k(0) is a substring of h_m(0), or report NIE.
Level

Hard8 of 10

Topics
String, Recursion, Divide and conquer, Implementation
Solved
No attempts yet

Problem

Define a function hh that acts on binary strings (strings over the digits 0 and 1). hh transforms a string by replacing every 0 with 1 and every 1 with the two-character string 10, with all replacements done simultaneously and independently. For example, hh maps 1001 to 101110, and it maps the empty string to the empty string. The function hh is injective. Let hkh_k denote hh composed with itself kk times; h0h_0 is the identity, so h0(w)=wh_0(w) = w.

Consider the one-character string 0 and the strings hk(0)h_k(0) for k=0,1,2,…k = 0, 1, 2, \dots. This sequence begins:

0, 1, 10, 101, 10110, 10110101, ...

A string xx is a substring of a string yy if it occurs in yy as a contiguous block. Given integers k1,k2,…,knk_1, k_2, \dots, k_n, decide whether the concatenation

hk1(0) hk2(0)⋯hkn(0)h_{k_1}(0)\,h_{k_2}(0)\cdots h_{k_n}(0)

is a substring of hm(0)h_m(0) for some mm, and if so find the smallest such mm.

Input

The first line contains one integer nn (1≤n≤1,000,0001 \le n \le 1{,}000{,}000). The second line contains nn non-negative integers k1,k2,…,knk_1, k_2, \dots, k_n (0≤ki≤1090 \le k_i \le 10^9), separated by single spaces.

Output

Print the smallest non-negative integer mm such that hk1(0) hk2(0)⋯hkn(0)h_{k_1}(0)\,h_{k_2}(0)\cdots h_{k_n}(0) is a substring of hm(0)h_m(0). If no such mm exists, print NIE.

Examples5

  1. Example 1

    Input
    2
    1 2
    
    Expected output
    4
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    5
    
    Expected output
    5
    
  4. Example 4

    Input
    2
    3 2
    
    Expected output
    4
    
  5. Example 5

    Input
    2
    2 0
    
    Expected output
    NIE