Equalize the Towers
Submissions: 420   Accuracy:

37.64%

  Difficulty: Easy   Marks: 2

Given heights of N towers. Your required to bring every tower to same height by either adding or removing blocks in a tower. Every addition or removal operation costs a particular value for respective tower. Find out the Minimum cost to Equalize the Towers.
Example:

Input : Tower heights h[] = {1, 2, 3}
Costs of operations cost[] = {10, 100, 1000}
Output : 120
The heights can be equalized by either "Removing 
one block from 3 and adding one in 1" or "Adding
two blocks in 1 and adding one in 2". Since the 
cost of operation in tower 3 is 1000, the first
process would yield 1010 while the second one 
yields 120. Since the second process yields the
lowest cost of operation, it is the required 
output.


Input:
First line of the input contains an integer T, denoting the number of test cases. Then T test case follows. First line of each test case contains an integer N, denoting the number of towers. Next line of test case contains N space separated integers denoting the height of the Towers. Third line also contains N space separated integers denoting the cost of an addition/removal operation for their respective tower.

Output:
For each test case output the Minimum cost required to Equalize the Towers.

Constraints:
1<=T<=100
3<=N<=103
1<=H[]<=105
0<=C[]<=105

Example:
Input:

2
3
2 4 8
10 100 1000

5
9 12 18 3 10
100 110 150 25 99
Output:
460
1623

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: harshitsidhwa


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.