Private Space

Time limit1sMemory limit128 MB

Summary
Choose the smallest widest row width X (at most 12) so that all groups fit into triangular rows of widths X down to 1, keeping one empty seat between neighboring groups in a row.
Level

Hard8 of 10

Topics
Dynamic programming, Bit manipulation, Greedy, Brute force
Solved
No attempts yet

Problem

People go to the cinema in groups (some go alone). Every group only wants to socialize within itself, so each group insists on at least one empty seat between itself and any neighbouring group in the same row — unless the group sits at one of the two ends of the row, where no empty seat is required on that side.

The cinema is triangular. If the widest row has XX seats, then the rows have X,X−1,X−2,…,1X, X-1, X-2, \dots, 1 seats (one row of each width from XX down to 11). Because of a capacity limit, the widest row may have at most 1212 seats.

The visitors are described by a list (N1,…,Nn)(N_1, \dots, N_n), where NiN_i is the number of groups that consist of exactly ii people. Every group must be seated in a single row (a group is never split across rows) and the seats a group occupies must be consecutive.

Find the smallest possible width XX of the widest row such that all groups can be seated at the same time while respecting the one-empty-seat rule.

Input

The first line contains a single integer nn (1≤n≤121 \le n \le 12): the size of the largest possible group.

The second line contains nn integers; the ii-th of them (1-indexed) is NiN_i, the number of groups of exactly ii people that must be seated.

Output

Print a single value: the smallest width XX of the widest row that seats everyone. If no width from 11 to 1212 can seat all groups, print impossible instead.

Examples2

  1. Example 1

    Input
    3
    0 1 1
    
    Expected output
    3
    
  2. Example 2

    Input
    3
    2 1 1
    
    Expected output
    4