CF1151D.Stas and the Queue at the Buffet

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of nn high school students numbered from 11 to nn . Initially, each student ii is on position ii . Each student ii is characterized by two numbers — aia_i and bib_i . Dissatisfaction of the person ii equals the product of aia_i by the number of people standing to the left of his position, add the product bib_i by the number of people standing to the right of his position. Formally, the dissatisfaction of the student ii , which is on the position jj , equals ai(j1)+bi(nj)a_i \cdot (j-1) + b_i \cdot (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.

输入格式

The first line contains a single integer nn ( 1n1051 \leq n \leq 10^5 ) — the number of people in the queue.

Each of the following nn lines contains two integers aia_i and bib_i ( 1ai,bi1081 \leq a_i, b_i \leq 10^8 ) — the characteristic of the student ii , initially on the position ii .

输出格式

Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.

输入输出样例

  • 输入#1

    3
    4 2
    2 3
    6 1
    

    输出#1

    12
  • 输入#2

    4
    2 4
    3 3
    7 1
    2 3
    

    输出#2

    25
  • 输入#3

    10
    5 10
    12 4
    31 45
    20 55
    30 17
    29 30
    41 32
    7 1
    5 5
    3 15
    

    输出#3

    1423

说明/提示

In the first example it is optimal to put people in this order: ( 3,1,23, 1, 2 ). The first person is in the position of 22 , then his dissatisfaction will be equal to 41+21=64 \cdot 1+2 \cdot 1=6 . The second person is in the position of 33 , his dissatisfaction will be equal to 22+30=42 \cdot 2+3 \cdot 0=4 . The third person is in the position of 11 , his dissatisfaction will be equal to 60+12=26 \cdot 0+1 \cdot 2=2 . The total dissatisfaction will be 1212 .

In the second example, you need to put people in this order: ( 3,2,4,13, 2, 4, 1 ). The total dissatisfaction will be 2525 .

首页