B - Balls of Three Colors Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

R 個の赤いボール,G 個の緑のボール,B 個の青いボールがあります. あなたは,以下の操作を好きな回数繰り返すことができます.

  • 色の異なる 2 つのボールを選び,それら両方を残るもう一つの色のボールに変える.

例えば,赤いボールと青いボールを選んだ際は,それら両方を緑のボールに変えます.

あなたの目標は,すべてのボールを同じ色にすることです. 目標が達成可能であるか判定し,また可能であるなら,必要な操作回数の最小値を求めてください.

1 つの入力ファイルにつき,T 個のテストケースを解いてください.

制約

  • 1 \leq T \leq 100
  • 1 \leq R,G,B \leq 10^8
  • 入力される値はすべて整数である

入力

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

T
case_1
case_2
\vdots
case_T

各ケースは以下の形式で与えられる.

R G B

出力

各ケースについて,目標が達成不可能な場合は -1 を,そうでなければ必要な操作回数の最小値を出力せよ.


入力例 1

3
1 2 2
1 2 3
1 2 4

出力例 1

2
-1
4

例えば,case_3 については,以下のように操作を行えばよいです.

  • 緑のボールと青いボールを選び,それら両方を赤いボールに変える
  • 赤いボールと青いボールを選び,それら両方を緑のボールに変える
  • 赤いボールと青いボールを選び,それら両方を緑のボールに変える
  • 赤いボールと青いボールを選び,それら両方を緑のボールに変える

Score : 400 points

Problem Statement

We have R red balls, G green balls, and B blue balls. You can do the following operation any number of times:

  • choose two balls of different colors and turn them into two balls of the remaining color.

For example, you can choose a red ball and a blue ball and turn them into two green balls.

Your objective is to make all balls have the same color. Determine whether this objective is achievable. If it is, find the minimum number of operations required to achieve it.

For each input file, solve T test cases.

Constraints

  • 1 \leq T \leq 100
  • 1 \leq R,G,B \leq 10^8
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
case_1
case_2
\vdots
case_T

Each case is in the following format:

R G B

Output

For each case, print -1 if the objective is unachievable; otherwise, print the minimum number of operations to achieve it.


Sample Input 1

3
1 2 2
1 2 3
1 2 4

Sample Output 1

2
-1
4

For example, in case_3, one optimal sequence of operations is:

  • choose a green ball and blue ball, turning them into two red balls;
  • choose a red ball and blue ball, turning them into two green balls;
  • choose a red ball and blue ball, turning them into two green balls;
  • choose a red ball and blue ball, turning them into two green balls.