Блог пользователя PRUGREMMER

Автор PRUGREMMER, история, 4 года назад, По-английски

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

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится