During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $n$ high school students numbered from $1$ to $n$. Initially, each student $i$ is on position $i$. Each student $i$ is characterized by two numbers — $a_i$ and $b_i$. Dissatisfaction of the person $i$ equals the product of $a_i$ by the number of people standing to the left of his position, add the product $b_i$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $i$, which is on the position $j$, equals $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.


-----Input-----

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

Each of the following $n$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq 10^8$) — the characteristic of the student $i$, initially on the position $i$.


-----Output-----

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


-----Examples-----
Input
3
4 2
2 3
6 1

Output
12
Input
4
2 4
3 3
7 1
2 3

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

Output
1423


-----Note-----

In the first example it is optimal to put people in this order: ($3, 1, 2$). The first person is in the position of $2$, then his dissatisfaction will be equal to $4 \cdot 1+2 \cdot 1=6$. The second person is in the position of $3$, his dissatisfaction will be equal to $2 \cdot 2+3 \cdot 0=4$. The third person is in the position of $1$, his dissatisfaction will be equal to $6 \cdot 0+1 \cdot 2=2$. The total dissatisfaction will be $12$.

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

Difficulty: interview

Starter code:

Solutions:

n = int(input())
S = []
for i in range(n):
    a, b = map(int, input().split())
    S.append([a, b])
S.sort(key=lambda x: (x[1] - x[0]))
cnt = 0
for i in range(n):
    cnt += S[i][0] * i + S[i][1] * (n - i - 1)
print(cnt)

n = int(input())
a = []
for i in range(n):
    a.append(list(map(int, input().split())))
a.sort(key=lambda x: x[1] - x[0])
ans = 0
for i in range(n):
    ans += a[i][0] * i
    ans += a[i][1] * (n - i - 1)
print(ans)

from collections import defaultdict as dd
import math
def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return list(map(int, input().split()))

def lm():
	return list(map(int, input().split()))

n=nn()

students=[]
for i in range(n):
	students.append(lm())

students.sort(key=lambda x: x[1]-x[0])

diss=0

for i in range(n):
	diss+=i*students[i][0]+(n-i-1)*students[i][1]

print(diss)


n=int(input())
que=[]
for i in range(n):
    a,b=[int(x) for x in input().split()]
    que.append((a-b,a,b))
que.sort(reverse=True)
counter=0
for i in range(n):
    counter+=que[i][1]*i+que[i][2]*(n-i-1)
print(counter)
    
    


n = int(input())
a = []
s = 0
for _ in range(n):
    t = list(map(int, input().split()))
    s += t[1] * n - t[0]
    a.append(t[0] - t[1])
a.sort()
for i in range(n):
    s += a[i] * (n - i)
print(s)


import sys
input = iter(sys.stdin.read().splitlines()).__next__

n = int(input())
A = [[int(x) for x in input().split()] for _ in range(n)]

L = []
R = []

cost = 0
for i in range(n):
    mn = min(A[i])
    cost += mn*(n-1)
    A[i][0] -= mn
    A[i][1] -= mn

    if A[i][0] == 0:
        R.append(A[i][1])
    elif A[i][1] == 0:
        L.append(A[i][0])

cost += sum(i*e for i,e in enumerate(sorted(L, reverse=True)))
cost += sum(i*e for i,e in enumerate(sorted(R, reverse=True)))

print(cost)


n = int(input())
s = []
for i in range(n):
    a,b = map(int, input().split())
    s.append((b-a,a,b))
s.sort()
ans = 0
for i in range(n):
    d,a,b = s[i]
    ans += a*i + b*(n-i-1)
print(ans)

import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = [list(map(int,input().split())) for i in range(n)]
for i in range(n):
    a[i].append(a[i][0]-a[i][1])
a.sort(key = lambda x:-x[2])
ans = 0
for i in range(n):
    ans += a[i][0]*i + a[i][1]*(n-i-1)
print(ans)


n = int(input())
values = []
for _ in range(n):
    a, b = list(map(int, input().split()))
    values.append((a, b))

values.sort(key=lambda x: x[0] - x[1], reverse=True)

diss = sum([values[i][0]*i + values[i][1]*(n-i-1) for i in range(n)])
print(diss)


import sys
input = sys.stdin.readline

N = int(input())
X = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    c = b - a
    X.append([a,b,c])

X = sorted(X, key = lambda x: x[2])
s = 0
for i in range(N):
    s += X[i][0] * i + X[i][1] * (N-i-1)

print(s)


n = int(input())
people = []
for i in range(n):
    people.append(tuple(int(x) for x in input().split()))
diss = sum(n*p[1]-p[0] for p in people) # the const part
a_b = list([p[0]-p[1] for p in people])
a_b.sort(reverse=True)
diss += sum((idx+1)*val for idx, val in enumerate(a_b))
print(diss)


def main():
    n = int(input())
    # left = []
    # right = []
    arr = []
    for i in range(n):
        x, y = map(int, input().split())
        arr.append((x, y))
        # if x > y:
        #     left.append((x, -y))
        # else:
        #     right.append((y, -x))
    ans = 0
    # left.sort(reverse = 1)
    # right.sort(reverse = 1)
    # for i in range(len(left)):
    #     x, y = left[i]
    #     ans += x * i - (n - 1 - i) * y
    # for i in range(len(right)):
    #     x, y = right[i]
    #     ans += x * i - (n - 1 - i) * y
    arr.sort(key = lambda a: a[1] - a[0])
    for i in range(n):
        x, y = arr[i]
        ans += x * i + (n - 1 - i) * y
    print(ans)
    return 0
main()

lines = int(input())

data = []
for i in range(lines):
    l, r = list(map(int, input().split(' ')))
    data.append((l, r))

data = sorted(data, key= lambda x: x[1] - x[0])

# calculate
t = 0
for i, (l, r) in enumerate(data):
    t += l * i + r * (lines - i - 1)

print(t)




n = int(input())
data = [list(map(int, input().split())) for _ in range(n)]
data.sort(key = lambda x: x[0]-x[1])
print(sum(v[1]*i + v[0]*(n-i-1) for i, v in enumerate(data)))

def key(ab):
    a, b = ab
    return a - b


def main():
    n = int(input())
    nm = 0
    m1 = n - 1
    res = 0
    for a, b in sorted((tuple(map(int, input().split())) for _ in range(n)), key=key):
        res += a * m1 + b * nm
        nm += 1
        m1 -= 1
    print(res)


main()


n = int(input())

a = []

for _ in range(n):
    ai, bi = list(map(int, input().split()))
    a.append((ai, bi, ai-bi))

a.sort(key = lambda x: x[2], reverse=True)

s = 0

for i, k in enumerate(a):
    s += k[0] * i + k[1] * (n - i -1)

print(s)



import sys
N = int(input())
alu = 0
A = []
B = []
for _ in range(N):
    a, b = map(int, sys.stdin.readline().split())
    alu += min(a, b)
    if a < b:
        B.append(b - a)
    else:
        A.append(a - b)
alu *= (N-1)
A.sort(reverse = True)
B.sort(reverse = True)
alu += sum([i*j for i, j in enumerate(A)]) + sum([i*j for i, j in enumerate(B)])
print(alu)

#21:34 18-04-2019
#21:44 18-04-2019
#21:53 18-04-2019

N_ = 100010

n = int(input())
a = []
b = []
d = []
sum = 0
for i in range(n):
	ai, bi = list(map(int, input().split()))
	a.append(ai)
	b.append(bi)
	d.append(ai-bi)
	sum += bi*n-ai
d.sort()
d = d[::-1]
for i in range(n):
	sum += d[i]*(i+1)

print(sum)

#21:51 18-04-2019
#21:53 18-04-2019


n = int(input())
arr = [list(map(int, input().split())) for _ in range(n)]
arr.sort(key=lambda x: (x[1] - x[0]))
res = 0
for i in range(n):
    res += arr[i][0] * i + arr[i][1] * (n - i - 1)
print(res)


import math
def cmp(a):
    return a[0] - a[1]
n = int(input())
res = 0
arr = []
for i in range(n):
    a, b = list(map(int, input().split()))
    arr.append((a, b))
arr.sort(key=cmp, reverse=True)
for i in range(len(arr)):
    res += arr[i][0] * i + arr[i][1] * (n - i - 1)
print(res)



N = int(input())

maps = []

for i in range(N):
    a, b = list(map(int, input().split()))
    diff = b - a
    maps += [[diff, a, b]]

maps.sort()

ans = 0

for i, v in enumerate(maps):
    a = v[1]
    b = v[2]
    j = i + 1
    
    ans += a * (j - 1) + b * (N - j)
print(ans)    


N = int(input())
ls = []
for i in range(N):
    ls.append(tuple(map(int, input().split())))
ls.sort(key=lambda x: x[1]-x[0])
sum = 0
for i in range(N):
    sum+=ls[i][0]*i+ls[i][1]*(N-1-i)
print(sum)


n = int(input())

pos = []
neg = []

res = 0
for i in range(n):
    a,b = list(map(int,input().split()))
    if a > b:
        pos.append([a, b])
    elif a < b:
        neg.append([a, b])
    else:
        res += (n - 1) * a

pos.sort(key=lambda l: -abs(l[0] - l[1]))
neg.sort(key=lambda l: -abs(l[0] - l[1]))

def f(i, l):
    return l[0] * (i - 1) + l[1] * (n - i)

for i in range(len(pos)):
    res += f(i + 1, pos[i])

for i in range(len(neg)):
    res += f(n - i, neg[i])

print(res)


def in_int():
    return int(input())
def in_list():
    return [int(x) for x in input().split()]

n=int(input())
ns=[]
ans=0
for i in range(n):
    x,y=in_list()
    ns.append(x-y)
    ans+=-x+y*n
ns.sort(reverse=True)
for i in range(n):
    c=ns[i]
    ans+=c*(i+1)
print(ans)