Permutations of numbers
Practice
3.5 (15 votes)
Sorting
Algorithms
Advanced sorting
Problem
68% Success 2634 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
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\) \((1 \leq p \leq N)\) at the \(i^{th}\) index, it costs \(L_p *(i - 1) + R_p*(N-i-1)\) 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
\(1 \leq T \leq 20\)
\(2 \leq N \leq 10^5\)
\(1 \leq L_i,R_i \leq 10^7\) for each \(1 \leq i \leq N\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
SortingAlgorithmsAdvanced Sorting
Points:30
6 votes
Tags:
C++SortingAdvanced SortingAlgorithms
Points:30
Tags:
SortingGreedy Algorithms
Editorial