Problem:
Score : 100 points

Problem Statement

Kode Festival is an anual contest where the hardest stone in the world is determined. (Kode is a Japanese word for "hardness".)

This year, 2^N stones participated. The hardness of the i-th stone is A_i.

In the contest, stones are thrown at each other in a knockout tournament.

When two stones with hardness X and Y are thrown at each other, the following will happen:

When X > Y:
The stone with hardness Y will be destroyed and eliminated.
The hardness of the stone with hardness X will become X-Y.

When X = Y:
One of the stones will be destroyed and eliminated.
The hardness of the other stone will remain the same.

When X < Y:
The stone with hardness X will be destroyed and eliminated.
The hardness of the stone with hardness Y will become Y-X.

The 2^N stones will fight in a knockout tournament as follows:

The following pairs will fight: (the 1-st stone versus the 2-nd stone), (the 3-rd stone versus the 4-th stone), ...

The following pairs will fight: (the winner of (1-st versus 2-nd) versus the winner of (3-rd versus 4-th)), (the winner of (5-th versus 6-th) versus the winner of (7-th versus 8-th)), ...

And so forth, until there is only one stone remaining.

Determine the eventual hardness of the last stone remaining.

Constraints

1 \leq N \leq 18

1 \leq A_i \leq 10^9

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1
A_2
:
A_{2^N}

Output

Print the eventual hardness of the last stone remaining.

Sample Input 1

2
1
3
10
19

Sample Output 1

7

Sample Input 2

3
1
3
2
4
6
8
100
104

Sample Output 2

2

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#define ll long long
#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define miin(a, b, c) min((a), min((b), (c)))
#define maax(a, b, c) max((a), max((b), (c)))
#define mod 1000000007

int n, m, a[333333], i = 2;

int nyaa(int x)
{
    int res = 1;
    while (x--)
        res *= 2;
    return res;
}

main()
{
    scanf("%d", &n);
    m = nyaa(n);
    for (int i = 0; i < m; i++)
    {
        scanf("%d", &a[i]);
    }
    while (n--)
    {
        for (int j = 0; j < m; j += i)
        {
            a[j] = (a[j] - a[i / 2 + j] ? abs(a[j] - a[i / 2 + j]) : a[j]);
        }
        i *= 2;
    }
    printf("%d\n", a[0]);
    return 0;
}