Chess For Three
Hazrat Ali
In every game, two of them play against each other. The winner gets 22 points while the loser gets 00, and in case of a draw, both players get 11 point each. Note that the same pair of players could have played any non-negative number of times (possibly zero). It is also possible that no games were played at all.
You've been told that their scores after all the games were played were p1p1, p2p2 and p3p3. Additionally, it is guaranteed that p1≤p2≤p3p1≤p2≤p3 holds.
Find the maximum number of draws that could have happened and print it. If there isn't any way to obtain p1p1, p2p2 and p3p3 as a result of a non-negative number of games between the three players, print −1−1 instead.
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001≤t≤500). The description of the test cases follows.
The first line of each test case contains three integers p1p1, p2p2 and p3p3 (0≤p1≤p2≤p3≤300≤p1≤p2≤p3≤30) — the scores of the three players, sorted non-decreasingly.
For each testcase, print one number — the maximum possible number of draws that could've happened, or −1−1 if the scores aren't consistent with any valid set of games and results.
0 1 -1 2 -1 6 2
Solution