AppPerfect question

The following question was asked in our campus for AppPerfect company. If possible someone can suggest a good approach

There are n shops of a certain product of given quantity on the x-axis. Each shop either buys the product, represented by +ve value or sells the product, represented by -ve value. You are initially at x = 0 and, our aim to travel minimum distance and satisfy the demands of all the shops who want to buy the product.

Input :
the first contains n, number of shops presents
the second line contains n integers containing the location of shos of the ith shop
the third line contains n integers containing the quantity of product it wants to buy or sell

Output :
A single integer which will be minimum distance by us to satisfy every demand

Sample:
input:
3

-10 1 2

5 -6 1

output:
14

Explanation:
we first go to from 0 to 1 for purchase and then to 2 for selling and then to -10 to sell again

Contraints -
1<= n <= 1e3,
-1e5 <= cooridnates <= 1e5
-1e5 <= demand <= 1e5

Could you please explain the sample test case. it is quite unclear

2 Likes

The sum of all values of demand array is zero always,right?

not necessary