Stas and the Queue at the Buffet
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of n n n high school students numbered from 1 1 1 to n n n. Initially, each student i i i is on position i i i. Each student i i i is characterized by two numbers — a i a_i ai and b i b_i bi. Dissatisfaction of the person i i i equals the product of a i a_i ai by the number of people standing to the left of his position, add the product b i b_i bi by the number of people standing to the right of his position. Formally, the dissatisfaction of the student i i i, which is on the position j j j, equals a i ⋅ ( j − 1 ) + b i ⋅ ( n − j ) a_i \cdot (j-1) + b_i \cdot (n-j) ai⋅(j−1)+bi⋅(n−j).
The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.
Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
Input
The first line contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1≤n≤105) — the number of people in the queue.
Each of the following n n n lines contains two integers a i a_i ai and b i b_i bi ( 1 ≤ a i , b i ≤ 1 0 8 1 \leq a_i, b_i \leq 10^8 1≤ai,bi≤108) — the characteristic of the student i i i, initially on the position i i i.
Output
Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.
Example
i n p u t \tt input input |
---|
3 4 2 2 3 6 1 |
o u t p u t \tt output output |
12 |
i n p u t \tt input input |
---|
4 2 4 3 3 7 1 2 3 |
o u t p u t \tt output output |
25 |
i n p u t \tt input input |
---|
10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15 |
o u t p u t \tt output output |
1423 |
Note
In the first example it is optimal to put people in this order: ( 3 , 1 , 2 3, 1, 2 3,1,2). The first person is in the position of 2 2 2, then his dissatisfaction will be equal to 4 ⋅ 1 + 2 ⋅ 1 = 6 4 \cdot 1+2 \cdot 1=6 4⋅1+2⋅1=6. The second person is in the position of 3 3 3, his dissatisfaction will be equal to 2 ⋅ 2 + 3 ⋅ 0 = 4 2 \cdot 2+3 \cdot 0=4 2⋅2+3⋅0=4. The third person is in the position of 1 1 1, his dissatisfaction will be equal to 6 ⋅ 0 + 1 ⋅ 2 = 2 6 \cdot 0+1 \cdot 2=2 6⋅0+1⋅2=2. The total dissatisfaction will be 12 12 12.
In the second example, you need to put people in this order: ( 3 , 2 , 4 , 1 3, 2, 4, 1 3,2,4,1). The total dissatisfaction will be 25 25 25.
Tutorial
设学生 i i i 所在位置为 j j j,则其不满意度贡献为 a i × ( j − 1 ) + b i × ( n − j ) a_i \times (j - 1) + b_i \times (n - j) ai×(j−1)+bi×(n−j),公式可变形为 ( a i − b i ) × j + b i × n − a i (a_i - b_i) \times j + b_i \times n - a_i (ai−bi)×j+bi×n−ai,由变形后的公式可以知道, b i × n − a i b_i \times n - a_i bi×n−ai 不会随着位置 j j j 而改变,则不满意贡献度只与 ( a i − b i ) × j (a_i - b_i) \times j (ai−bi)×j 有关
令 c i = a i − b i c_i = a_i - b_i ci=ai−bi,则对于任意两个位置 i , j ( i < j ) i,j(i < j) i,j(i<j),且 c i < c j c_i < c_j ci<cj,如果此时交换两个数,则不满意度变化为 ( c i × j + c j × i ) − ( c i × i + c j × j ) (c_i \times j + c_j \times i) - (c_i \times i + c_j \times j) (ci×j+cj×i)−(ci×i+cj×j),即 ( c i − c j ) × ( j − i ) < 0 (c_i - c_j) \times (j - i) < 0 (ci−cj)×(j−i)<0,此调整使总体不满意度变小,所以如果存在任意两个位置 i , j ( i < j ) i,j(i < j) i,j(i<j),且 c i < c j c_i < c_j ci<cj,则交换这两个元素,即顺序为根据 a i − b i a_i - b_i ai−bi 进行逆序排序
此解法时间复杂度为 O ( n log n ) \mathcal O(n \log n) O(nlogn)
Solution
import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
a, b = [], []
for _ in range(n):
ai, bi = map(int, input().split())
a.append(ai)
b.append(bi)
idx = list(range(n))
idx.sort(reverse = True, key = lambda x: a[x] - b[x])
print(sum(i * a[idx[i]] + (n - i - 1) * b[idx[i]] for i in range(n)))