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<=10^{3}

1<=H[]<=10^{5}

0<=C[]<=10^{5}

**Example:
Input:**

2

3

2 4 8

10 100 1000

5

9 12 18 3 10

100 110 150 25 99

460

1623

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.

preparecodinginterview_dot_com | 190 |

__murugan__ | 171 |

manvimahajan20 | 134 |

ShameekAgarwal | 116 |

SoumyaKaushik | 108 |

preparecodinginterview_dot_com | 190 |

__murugan__ | 171 |

manvimahajan20 | 160 |

SoumyaKaushik | 146 |

arpit_anshuman | 140 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.