N soldiers problem

There are N soldiers located on our X-AXIS. The point at which soldier is located also has some number of bombs.
The war is near and every soldier wants to communicate with every other soldier.
If the ith soldier has b number of bombs and is located at position X then the cost of communicating with any other soldier j having c number of bombs located at position Y is defined as |X-Y|*max(b,c).
Find the sum of costs of communication if every soldier wants to communicate with every other soldier.
NOTE :- You have to consider pair(i,j) only once in sum of costs.

Input Format
First line consists of number of test cases T. Each test case consists of three lines. The first line indicates the number of soldiers (N). The second line indicates the coordinates of the N soldiers ( X[i] ). The third line contains the number of bombs at every soldiers location ( B[i] ) . The x-coordinates needn’t be in increasing order in the input.

Constraints
1 <= T <= 20 1 <= N <= 200000 1 <= X[i] <= 1000000000 1 <= B[i] <= 10000

Output Format
The total cost modulo 10^9+7.

Sample Input
1
3
1 3 6
10 20 30
Sample Output
280
Explanation
there are 3 pairs (1,2) -> cost = abs(3-1) * 20 = 40 (1,3) -> cost = abs(1-6) * 30 = 150 (2,3) -> cost = abs(3-6) * 30 = 90 sum = 40 + 150 + 90 = 280

I HAVE ATTEMPTED THIS QUESTION THE SAME AS RAMU’S PROBLEM BUT AFTER GETTING CORRECT ANS MY ALL TEST CASES GOT FAILED?


PLZ RESOLVE

Hey @aditi2512201012019
This question is different from Ramu Problem. It is a competitive programming problem. Firstly you need to use segment trees or fenwick tree for this since your solution is N^2 but solution needs to be NlogN. Then you are using int you need to use long long. And you forgot to use modulo as well.

okay so it wont be done under this topics which i have covered?

@aditi2512201012019
Unfortunately it is an advanced topic and is covered only in competitive programming.
You can still read up about it yourself


If your doubt is resolved please mark it as closed.