405111: GYM101798 F World Mug (A)

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

F. World Mug (A)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

The 2018 WIFA World Mug is around the corner.

The tournament consists of n teams. In each round of the tournament, each team plays against another team. A team that loses a match exits the tournament, while a team that wins a match progresses to the next round. The tournament finishes when one team wins the final match.

In the first round, team 1 plays against team 2, team 3 plays against team 4, and so on. In the second round, the winner of the first match plays against the winner of the second match, and so on as shown in the picture.

Each team has a certain unique strength si. Team i will win a match against team j if its strength si is greater than team j's strength sj. The number of goals scored in that match equals |si - sj|.

Given the tournament format, can you calculate the number of goals scored throughout the tournament?

Input

The first line of input contains one integer n (2 ≤ n ≤ 218), the number of teams participating in the tournament. It is guaranteed that the number of teams is a power of two (i.e., 2, 4, 8, 16, ...).

The second line contains n distinct integers, the ith integer is si (1 ≤ si ≤ 109), the strength of the ith team.

Output

On a single line, print the number of goals that will be scored in the tournament.

ExampleInput
8
100 200 300 400 800 700 600 500
Output
1200

加入题单

算法标签: