hanoig

Let's take 3 rods (A, B and C). Rod A contains n disks of k distinct sizes d1>d2>...>dk.
More specifically, there are m1 disks of diameter d1, m2 disks of diameter d2, ..., mk disks of diameter dk.
Obviously, m1+m2+...+mk=n.
The disks are initially placed on rod A in descending size order, from the base to the top (so at the base we have the m1 disks of diameter d1, then the m2 disks of diameter d2, ...).
During a move a single disk can be moved from one rod to another, but never will a larger diameter disk be placed over a smaller diameter disk.
The purpose is to move all disks from rod A to rod B, permanently respecting descending size order.
Rod C can be used as a maneuver rod.

Task

Write a program that determines the minimum number of moves that have to be executed to move the n disks from rod A to rod B.

Input Data

Input file hanoig.in contains on the first line a positive integer k, with the meaning in the task description. The second line will contain k positive integers each separated by a space, m1 m2 ... mk.

Output Data

Output file hanoig.out will contain a single line with the minimum number of moves which have to be executed to move the n=m1+m2+...+mk disks from rod A to rod B.

Constraints and Statements

Example
hanoig.in hanoig.out

2
2 3

8

Time limit: 0.1 seconds/test

prof. Emanuela Cerchez
"Grigore Moisil" Iaşi IT High School
Contact:emanuela.cerchez@gmail.com