C - Kode Festival Editorial /

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100100

問題文

「硬度フェスティバル」は毎年開催される、世界で一番硬い石を決める大会です。

今年の硬度フェスティバルには 2N2^N 個の石が参加します。 ii 番目の石の硬度は AiA_i です。

大会では石をトーナメント形式でぶつけ合って、最硬の石を決めます。

硬度 XX の石と硬度 YY の石をぶつけると以下のような結果になります。

  • XX > YY のとき: 硬度が YY だった石は砕けて、 硬度が XX だった石の硬度は XYX-Y になります。 このとき硬度が XX だった石が勝ち残ります。

  • XX = YY のとき: どちらかの石が砕けます。もう片方の石が硬度が元と変わらないまま残ります。このとき砕けなかった方の石が勝ち残ります。

  • XX < YY のとき: 硬度が XX だった石は砕けて、 硬度が YY だった石の硬度は YXY-X になります。 このとき硬度が YY だった石が勝ち残ります。

2N2^N 個の石は以下のようなトーナメント形式で勝負をします。

  1. (11 番目の石、22 番目の石)、(33 番目の石、44 番目の石)、... の組み合わせでぶつけ合う。

  2. ((1,2)(1, 2) の勝ち残り、(3,4)(3, 4) の勝ち残り)、((5,6)(5, 6) の勝ち残り、(7,8)(7, 8) の勝ち残り)、... の組み合わせでぶつけ合う。

  3. 同様に、勝ち残りが 11 つだけになるまで続ける。

最後まで勝ち残る石の、最後の時点での硬度を求めてください。

制約

  • 1N181 \leq N \leq 18
  • 1Ai1091 \leq A_i \leq 10^9
  • AiA_i は整数である。

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1
A2A_2
::
A2NA_{2^N}

出力

最後まで勝ち残る石の、最後の時点での硬度を出力せよ。


入力例 1Copy

Copy
2
1
3
10
19

出力例 1Copy

Copy
7

入力例 2Copy

Copy
3
1
3
2
4
6
8
100
104

出力例 2Copy

Copy
2

Score : 100100 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, 2N2^N stones participated. The hardness of the ii-th stone is AiA_i.

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

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

  • When XX > YY: The stone with hardness YY will be destroyed and eliminated. The hardness of the stone with hardness XX will become XYX-Y.

  • When XX = YY: One of the stones will be destroyed and eliminated. The hardness of the other stone will remain the same.

  • When XX < YY: The stone with hardness XX will be destroyed and eliminated. The hardness of the stone with hardness YY will become YXY-X.

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

  1. The following pairs will fight: (the 11-st stone versus the 22-nd stone), (the 33-rd stone versus the 44-th stone), ...

  2. The following pairs will fight: (the winner of (11-st versus 22-nd) versus the winner of (33-rd versus 44-th)), (the winner of (55-th versus 66-th) versus the winner of (77-th versus 88-th)), ...

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

Determine the eventual hardness of the last stone remaining.

Constraints

  • 1N181 \leq N \leq 18
  • 1Ai1091 \leq A_i \leq 10^9
  • AiA_i is an integer.

Input

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

NN
A1A_1
A2A_2
:
A2NA_{2^N}

Output

Print the eventual hardness of the last stone remaining.


Sample Input 1Copy

Copy
2
1
3
10
19

Sample Output 1Copy

Copy
7

Sample Input 2Copy

Copy
3
1
3
2
4
6
8
100
104

Sample Output 2Copy

Copy
2


2025-04-07 (Mon)
13:52:36 +00:00