cswork / OAOpen practice workspace

CiscoOA #4

Binary Linked List to Decimal

Estimated EasyJudge-ready

Topics (AI-labelled)Linked ListsBits & Math

Each node of a linked list stores 0 or 1, and the head node holds the most significant bit. The input is the sequence of values from head to tail, and leading 0s are allowed; output the corresponding exact non-negative decimal integer. You do not need to build actual linked list nodes in your program.

Input

The first line contains N, and the second line contains N values of 0/1. Original bound: 1≤N≤64. The maximum answer is 2^64−1=18446744073709551615; it cannot be replaced by a signed 64-bit overflow result, and the most significant bit is not restricted to 0.

Output

Output a single integer, the answer.

Example 1

Input:

7
0 0 1 1 0 1 0

Output:

26

Example 2

Input:

3
1 0 0

Output:

4

Example 3

Input:

1
0

Output:

0

Example notes

The outputs of the three public examples are, in order: 26; 4; 0. Verified by independent enumeration or direct simulation.

Editorial

Loading solution…