atcoder#AGC045E. [AGC045E] Fragile Balls
[AGC045E] Fragile Balls
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.
3 3
1 2 1
2 1 1
1 3 2
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 .
2 2
1 2 1
2 1 1
-1
5 5
1 2 1
2 1 1
1 3 2
4 5 1
5 4 1
6
1 1
1 1 1
0