# Crying Colours CRYCOLR Solution Codechef

## Crying Colours CRYCOLR Solution

You have a total of 3N balls of colours Red, Blue and Green. There are exactly N balls of each colour. These 3N balls are now distributed to 3 boxes such that each box contains exactly N balls. You are given the contents of each box.

You would like the 1st box to contain all the red balls, the 2nd box to contain all the green balls, and the 3rd 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.

Input Format

• The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
• Each test case consists of 4 lines of input.
• The first line of each test case contains a single integer N, denoting the number of balls of each colour.
• The i-th of the next three lines contains three space-separated integers Ri,Gi and Bi the number of red, green, and blue balls in the i-th box respectively.

Output Format

For each test case, output a single line containing one integer the minimum number of operations required such that the given condition is satisfied.

Constraints

• 1≤T≤1000
• 1≤N≤1000
• 0≤Ri,Gi,Bi≤N
• R1+G1+B1=R2+G2+B2=R3+G3+B3=N
• R1+R2+R3=G1+G2+G3=B1+B2+B3=N

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

``````0
3``````

Explanation

Test case 1: Initially,

• The first box has 3 red balls and none of any other colours
• The second box has 3 green balls and none of any other colours
• The third box has 3 blue balls and none of any other colours

The condition is already satisfied, and so no moves are required.

Test case 2: 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.

### SOLUTION

Program: Crying Colours CRYCOLR Solution in C++

``````#include <iostream>
#include <algorithm>
#include<cstdlib>
using namespace std;
int main() {
short t,n;
cin>>t;
while(t--){
cin>>n;
short arr1[3],arr2[3],arr3[3];
for(int i=0;i<3;i++) cin>>arr1[i];
for(int i=0;i<3;i++) cin>>arr2[i];
for(int i=0;i<3;i++) cin>>arr3[i];
short result=min(arr1[1],arr2[0])+min(arr1[2],arr3[0])+min(arr2[2],arr3[1]);
result+=(2*abs(arr1[1]-arr2[0]));
cout<<result<<"\n";
}
return 0;
}``````

Program: Crying Colours CRYCOLR Solution in Java

``````import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while (T-- > 0) {
int N = in.nextInt();
int [][]arr= new int[3][3];
for (int i=0;i<3;i++){
for(int j=0;j<3;j++){
arr[i][j]=in.nextInt();
}
}
int count=0;
if(arr[0][0]==N && arr[1][1]==N && arr[2][2]==N){
System.out.println(count);
continue;
}
int a=arr[0][1]+arr[0][2]+arr[1][2];
int b=arr[1][0]+arr[2][0]+arr[2][1];
System.out.println(Math.max(a,b));
}
}
}``````

Program: Crying Colours CRYCOLR Solution in Python

``````for _ in range(int(input())):
n=int(input())
a=[]
for i in range(3):
b=list(map(int,input().split()))
a.append(b)
if(a[0][0]==n and a[1][1]==n and a[2][2]==n):
print(0)
continue
c=a[1][0]+a[2][0]+a[2][1]
b=a[0][1]+a[0][2]+a[1][2]
print(max(c,b))``````