Codeforces Round 553 (Div. 2) D. Stas and the Queue at the Buffet

Stas and the Queue at the Buffet

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

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(j1)+bi(nj).

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 1n105) — 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 1ai,bi108) — 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 41+21=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 22+30=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 60+12=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×(j1)+bi×(nj),公式可变形为 ( a i − b i ) × j + b i × n − a i (a_i - b_i) \times j + b_i \times n - a_i (aibi)×j+bi×nai,由变形后的公式可以知道, b i × n − a i b_i \times n - a_i bi×nai 不会随着位置 j j j 而改变,则不满意贡献度只与 ( a i − b i ) × j (a_i - b_i) \times j (aibi)×j 有关

c i = a i − b i c_i = a_i - b_i ci=aibi,则对于任意两个位置 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 (cicj)×(ji)<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 aibi 进行逆序排序

此解法时间复杂度为 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)))
  • 24
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值