Stoned Game
Contest: Codeforces Round 666 (Div. 1)
Difficulty rating: 1800
Tags: brute force, constructive algorithms, games, greedy

T is playing a game with his friend, HL.

There are $$$n$$$ piles of stones, the $$$i$$$-th pile initially has $$$a_i$$$ stones.

T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.

Assuming both players play optimally, given the starting configuration of $$$t$$$ games, determine the winner of each game.

Input format:
The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of games. The description of the games follows. Each description contains two lines:

The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$ — the number of piles.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 100)$$$.

Output format:
For each game, print on a single line the name of the winner, "T" or "HL" (without quotes)

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains $$$1$$$ stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.

Example input:
2
1
2
2
1 1

Example output:
T
HL

Editorial:
Let us denote $$$S$$$ as the current total number of stones.
Consider the following cases:
• Case A: There is a pile that has more than $$$\lfloor \frac{S}{2} \rfloor$$$ stones.The first player (T) can always choose from this pile, thus he (T) is the winner.
• Case B: Every pile has at most $$$\lfloor \frac{S}{2} \rfloor$$$ stones, and $$$S$$$ is even.It can be proven that the second player (HL) always wins.Proof 1: Let us prove by induction:When $$$S = 0$$$, the second player obviously wins. When $$$S \geq 2$$$, consider the game state after the first player moves. If there is a pile that now has more than $$$\lfloor \frac{S}{2} \rfloor$$$ stones, then we arrive back at case A where the next player to move wins. Otherwise, the second player can choose from any valid pile (note that the case condition implies that there are at least two non-empty piles before the first player's move). Now $$$S$$$ has been reduced by $$$2$$$, and every pile still has at most $$$\lfloor \frac{S}{2} \rfloor$$$ stones.Proof 2: The condition allows us to assign a perfect matching of stones, where one stone is matched with exactly one stone from a different pile. A greedy way to create such a matching: Give each label $$$0, 1, \dots, S - 1$$$ to a different stone so that for every pair of stones with labels $$$l < r$$$ that are from the same pile, stones $$$l + 1, l + 2, \dots, r - 1$$$ are also from that pile; then match stones $$$i$$$ with $$$i + \frac{S}{2}$$$ for all $$$0 \le i < \frac{S}{2}$$$.For every stone that the first player removes, the second player can always remove its matching stone, until the first player can no longer make a move and loses.
• Case C: Every pile has at most $$$\lfloor \frac{S}{2} \rfloor$$$ stones, and $$$S$$$ is odd.The first player (T) can choose from any pile, and we arrive back at case B where the next player to move loses.
So the first player (T) wins if and only if there is a pile that has more than $$$\lfloor \frac{S}{2} \rfloor$$$ stones or $$$S$$$ is odd. This can be easily checked in $$$O(n)$$$.