#P2531. Elimination Tournament

    ID: 15801 Type: Default 1000ms 256MiB

Elimination Tournament

Elimination Tournament

In an elimination tournament, three countries (A, B, and C) compete. Each country has a line‐up of players numbered in order (starting from 1) with distinct strength values. The tournament rules are as follows:

  1. Before round 1, a team is chosen by drawing lots to sit out. Then the match of round 1 is played between the first (i.e. 1‑numbered) players of the two teams that did not get the bye.
  2. In every match, the player with the higher strength wins and the loser is eliminated from his team.
  3. For every subsequent round, the match is contested between the winner (the "champion" who stays on) from the previous round and the player with the smallest available number from the team that sat out in the previous round.
  4. After each match, the team that did not participate in the current round (i.e. the one that sat out in the previous round) becomes the waiting team for the next round.
  5. The tournament continues until only one country has any players left. That country is declared the champion.

For example, let the strengths be as follows (each team’s players are listed in order):

  • Team A: 1, 3
  • Team B: 2, 5
  • Team C: 4, 6

If team C is chosen to sit out in round 1, then:

  • Round 1: Team A’s player 1 (strength 1) faces Team B’s player 1 (strength 2). The latter wins, so Team A loses its first player.
  • Round 2: The previous winner from Team B (strength 2) faces the first player of the waiting team (Team C’s player 1 with strength 4). Team C wins, eliminating Team B’s player.
  • Round 3: Now, the match is between Team C’s current champion (strength 4) and the smallest-numbered remaining player from the team that sat out in round 2 (which is Team A’s player 2 with strength 3). Team C wins.
  • Round 4: The match is between Team C’s champion (strength 4) and the next available player from the waiting team (Team B’s player 2 with strength 5). Team B wins.
  • The rounds continue until the players of two teams are completely eliminated. In this example, the final winner is Team B.

Note: All strength values are distinct so that there are no ties. Matches are decided by comparing the strengths (using the inequality \(a > b\)).

Your task is to simulate the tournament and output the winning country.

inputFormat

The input consists of:

  1. The first line contains three integers \(A\), \(B\), and \(C\) (\(1 \le A, B, C \le 10^5\)) which represent the number of players in teams A, B, and C respectively.
  2. The second line contains \(A\) space-separated integers representing the strength values for team A in order (from player 1 to player A).
  3. The third line contains \(B\) space-separated integers representing the strength values for team B.
  4. The fourth line contains \(C\) space-separated integers representing the strength values for team C.
  5. The fifth line contains a single uppercase letter (either "A", "B", or "C") indicating which team gets the bye (i.e. sits out) in round 1.

outputFormat

Output a single uppercase letter: "A", "B", or "C" indicating the winning team.

sample

2 2 2
1 3
2 5
4 6
C
B