#P6067. Sum of Chatting Volumes

    ID: 19291 Type: Default 1000ms 256MiB

Sum of Chatting Volumes

Sum of Chatting Volumes

Farmer John's farm has N cows (1 ≤ N ≤ 10^5). The i-th cow is located at position x_i (0 ≤ x_i ≤ 10^9).

Each cow chats with every other cow. When cow i chats with cow j, the volume of conversation is \( |x_i - x_j| \). Note that the conversation between two cows is counted twice (once from each cow's perspective).

Your task is to compute the total chatting volume among all cows.

Hint: If the cows' positions are sorted as \( x_1 \le x_2 \le \cdots \le x_N \), then the sum of volumes for each distinct pair \( (i,j) \) with \( i<j \) is given by:

\[ S = \sum_{i=1}^{N} (2i - N - 1)x_i, \]

Since each conversation is counted twice, the final answer is \( 2\times S \).

inputFormat

The first line contains an integer N representing the number of cows.

The second line contains N space-separated integers, where the i-th integer represents the position \( x_i \) of the i-th cow.

outputFormat

Output a single integer denoting the total chatting volume among all cows.

sample

2
0 1
2