5. The Water Boy

5

2 votes
Medium
Problem

X is a person who likes equality. He has n jars of infinite height in front of him and wants to equalize the water in all the jars. Each jar initially contains some litres of water. X can remove or add any amount of litres to any of the jar so as to equalize the content of all the jars. X can only remove or add integer amount of litres only. But for adding or removing water from any jar, there is a cost associated with it and X wants to minimize this cost. Help him in accomplishing the task.

h = initial water content in each jar
c = cost of adding or removing 1 litre of water from the respective jar

Constraints:
1<=t<=10
0<=n<=10000
0<=h<=10000
0<=c<=10000

Input format:
First line contains integer t.
Next follows 3*t lines.
First line contains n
Second line contains n integers denoting h
Third line contains n integers denoning c

Output format:
For each test case print a single integer denoting the answer

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

1 litre of water is removed from third jar and 1 litre is added to to the first jar. So cost = 3+1 = 4.

Editor Image

?