

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
から までの番号の付いた 個の箱があります. また, から までの番号の付いた 個のボールがあります. 現在,ボール は箱 に入っています.
あなたは,以下の操作を行えます.
- 現在ボールが 個以上入っている箱を つ選ぶ. そして,その箱からボールを つ選んで取り出し,別の箱に入れる.
ただし,ボールは非常に壊れやすいため,ボール は合計で 回より多く移動させることはできません. 逆に,ボールが壊れない限り,何度でもボールの移動は行なえます.
あなたの目標は,すべての ()について,ボール が箱 に入っているようにすることです. この目的が達成可能かどうか判定してください. また可能な場合は,目標を達成するのに必要な操作回数の最小値を求めてください.
制約
- 目標とする状態において,どの箱にも つ以上のボールが入っている. つまり,すべての () について, を満たす が存在する.
入力
入力は以下の形式で標準入力から与えられる.
出力
目標が達成不可能な場合は を,達成可能な場合は必要な操作回数の最小値を出力せよ.
入力例 1Copy
3 3 1 2 1 2 1 1 1 3 2
出力例 1Copy
3
以下のように 回の操作を行えば良いです.
- 箱 からボール を取り出し,箱 に入れる.
- 箱 からボール を取り出し,箱 に入れる.
- 箱 からボール を取り出し,箱 に入れる.
入力例 2Copy
2 2 1 2 1 2 1 1
出力例 2Copy
-1
入力例 3Copy
5 5 1 2 1 2 1 1 1 3 2 4 5 1 5 4 1
出力例 3Copy
6
入力例 4Copy
1 1 1 1 1
出力例 4Copy
0
Score : points
Problem Statement
We have boxes numbered to , and balls numbered to . Currently, Ball is in Box .
You can do the following operation:
- Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.
Since the balls are very easy to break, you cannot move Ball more than times in total. Within this limit, you can do the operation any number of times.
Your objective is to have Ball in Box for every (). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it.
Constraints
- In the situation where the objective is achieved, every box contains one or more balls. That is, for every (), there exists such that .
Input
Input is given from Standard Input in the following format:
Output
If the objective is unachievable, print ; if it is achievable, print the minimum number of operations required to achieve it.
Sample Input 1Copy
3 3 1 2 1 2 1 1 1 3 2
Sample Output 1Copy
3
We can achieve the objective in three operations, as follows:
- Pick up Ball from Box and put it into Box .
- Pick up Ball from Box and put it into Box .
- Pick up Ball from Box and put it into Box .
Sample Input 2Copy
2 2 1 2 1 2 1 1
Sample Output 2Copy
-1
Sample Input 3Copy
5 5 1 2 1 2 1 1 1 3 2 4 5 1 5 4 1
Sample Output 3Copy
6
Sample Input 4Copy
1 1 1 1 1
Sample Output 4Copy
0