

The intersection of the set of the persons who go to ‘CITY_A’ to those going to ‘CITY_B’ is to be disjoint set, whereas the union must be the ‘2N’.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the 'T' test cases follow.
The first line of each test case contains a positive integer, ‘N’, such that ‘2N’ is the number of people coming to the exhibition.
The next ‘2N’ lines of each test case contain two integers, ‘COST_TO_Ai’, and ‘COST_TO_Bi’, as described in the problem statement.
The ‘COST’ will be passed to the function as an array of arrays. Each array will contain two integers, ‘COST_TO_Ai’, and ‘COST_TO_Bi’ in that order.
For each test case, return the minimum cost of organizing the exhibition, as described in the problem statement.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 50
COST.length == 2*N
1 <= CostToAi, CostToBi <= 10^4
Time Limit: 1sec
The idea here is to use the recursion. For every ‘ith’ person, we have two options available. The first one is to fly to ‘City A’ for the cost of ‘COST_TO_Ai’, and the second is to fly to ‘City B’ for the cost of ‘COST_TO_Bi’. If at any point we have sent ‘N’ persons to one city, then we have to send the remaining peoples to the other city only. We have to always return the minimum in every function call. To avoid the function to again compute the result for the same function call, we can save the previously called function’s output that is nothing but the memoization.
The idea here is to use the greedy approach. First, we send all persons to ‘CITY_A’ and calculate the total cost of doing so. Now, we want ‘N’ persons to go to ‘CITY_B’ from ‘CITY_ A’ so that our total cost would be minimal. So, if we send the ith person from ‘CITY_A’ to 'CITY_B', we have to subtract ‘COST_TO_A’ and add ‘COST_TO_B’. In other words, we have to calculate the value of “COST_TO_Bi - COST_TO_Bi” for all ‘i’, and store it into a new array. After that, we have to sort the array in the non-decreasing order and just send the starting ‘N’ persons to ‘CITY_B’.