Crying Colours solution codechef
You have a total ofballs of colours , and . There are exactly balls of each colour.
Theseballs are now distributed to boxes such that each box contains exactly balls. You are given the contents of each box.
You would like thebox to contain all the red balls, the box to contain all the green balls, and the box to contain all the blue balls. Note that the given order of boxes is important here — it is not enough for each box to contain only balls of a single colour.
To achieve this, you can perform the following operation several (possibly, zero) times:
- Pick any two distinct boxes, pick any one ball from each of these two boxes, and swap them.
Determine the minimum number of operations required to satisfy the given condition.
- The first line of input contains a single integer , denoting the number of test cases. The description of test cases follows.
- Each test case consists of lines of input.
- The first line of each test case contains a single integer , denoting the number of balls of each colour.
- The -th of the next three lines contains three space-separated integers and — the number of red, green, and blue balls in the -th box respectively.
For each test case, output a single line containing one integer — the minimum number of operations required such that the given condition is satisfied.
Subtask #1 (100 points): Original constraints
Sample Input 1
2 3 3 0 0 0 3 0 0 0 3 5 2 1 2 1 4 0 2 0 3
Sample Output 1
Test case: Initially,
- The first box has red balls and none of any other colours
- The second box has green balls and none of any other colours
- The third box has blue balls and none of any other colours
The condition is already satisfied, and so no moves are required.
Test case: One sequence of moves is as follows:
- Swap a green ball from the first box with a red ball from the second box.
- Swap a blue ball from the first box with a red ball from the third box.
- Once again, swap a blue ball from the first box with a red ball from the third box.
Now the first box has only red balls, the second has only green balls, and the third has only blue ones — as required. It can be verified that no sequence of less than three moves achieves this result.