P26377 [AtCoder] Pair Distance
提高-
题目描述

Problem Description

In a city that can be represented as a number line, there are $ N $ people living at coordinates $ x_i $. No two people live at the same coordinate.

The cost required for communication between a person living at coordinate $ x $ and a person living at coordinate $ y $ is $ |x-y| $.

The total communication cost for the entire city is calculated as the sum of the costs for all pairs of people $1 \leq i < j \leq N$ to communicate.

Output this total communication cost.

Input Format

The input is given from the standard input in the following format:

$ N $ $ x_1 $ $ x_2 $ ... $ x_{N-1} $ $ x_N $

Output Format

Output the total communication cost of the city.

Sample Input and Output

Sample Input #1

2
-2 3

Sample Output #1

5

Sample Input #2

3
10 1 5

Sample Output #2

18

Notes/Hints

Constraints

  • $2 \leq N \leq 10^5$
  • $0 \leq |x_i| \leq 10^8$
  • All $ x_i $ are distinct
  • All inputs are integers

Sample Explanation 1

The cost for communication between the person at $ -2 $ and the person at $ 3 $ is $ 5 $.

Sample Explanation 2

The cost for communication between the person at $ 10 $ and the person at $ 1 $ is $ 9 $. The cost for communication between the person at $ 10 $ and the person at $ 5 $ is $ 5 $. The cost for communication between the person at $ 1 $ and the person at $ 5 $ is $ 4 $. Therefore, the total communication cost for the city is $ 18 $.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 -2 3
样例输出
5