#P4715. Determining the Tournament Runner-Up

    ID: 17959 Type: Default 1000ms 256MiB

Determining the Tournament Runner-Up

Determining the Tournament Runner-Up

In a knockout tournament, there are \(2^n\) (with \(n \le 7\)) teams participating, each with a unique capability value. In each match, the team with the higher capability wins. The matches are held in a fixed bracket: team 1 plays team 2, team 3 plays team 4, and so on. The winners advance to the next round following the same pairing until a champion is determined.

Since the bracket is fixed, the tournament is divided into two halves: the left half (teams 1 to \(2^{n-1}\)) and the right half (teams \(2^{n-1}+1\) to \(2^n\)). The final match is played between the winners of these halves. Given that the outcome is solely decided by the capability values, the overall champion is the team with the highest capability. The runner-up, which is the team that loses the final, is therefore the strongest team from the opposite half of the champion.

Your task is to determine the number (index) of the runner-up country.

inputFormat

The input consists of two lines:

  1. The first line contains a single integer \(n\), where \(n \le 7\).
  2. The second line contains \(2^n\) space-separated distinct integers representing the capabilities of the countries in order (from country 1 to country \(2^n\)).

outputFormat

Output a single integer representing the number (index) of the country that ends as the runner-up.

sample

1
5 3
2