Permutations of numbers

3.5

15 votes
Sorting, Algorithms, Advanced Sorting
Problem

There are N numbers from 1 to N and your task is to create a permutation such that the cost of the permutation is minimum. For each number, there is a left and right cost. To put number  p (1pN) at the ith index, it costs Lp(i1)+Rp(Ni1) where L[] and R[] cost is given.

You do not need to find that permutation but you need to find the total minimum possible cost of the permutation.

Input format

  • The first line contains T denoting the number of test cases.
  • The first line of each test case contains the number N.
  • The next line of each test case contains N integers to indicate the left cost. 
  • The next line of each test case contains N integers to indicate the right cost.

Output format

For each test case, print a single line containing one integer that denotes the minimum cost of the permutation.

Constraints

1T20

2N105

1Li,Ri107 for each 1iN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the test case 1, The possible permutaion will be 3,1,2. The cost will be (06+22)+(11+14)+(22+07)=13 and this is the minimum possible value.

For the test case 2, The possible permutaion will be 1,4,3,2. The cost will be (04+33)+(12+22)+(23+15)+(35+08)=41 and this is the minimum possible value.

 

Editor Image

?