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$.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Sorting

Mathematics

Greedy algorithms


Solutions:
n = int(input())
ab = [(0, 0)] * n
for i in range(n):
	ab[i] = tuple(map(int, input().split()))
	ab[i] = (ab[i][0], ab[i][1], ab[i][0] - ab[i][1])
ab = sorted(ab, key=lambda x: x[2], reverse=True)
sm = 0
for i in range(n):
	sm += -ab[i][0] + n * ab[i][1] + (i + 1) * ab[i][2]
print(sm)


n = int(input())
vec = []
for i in range(n):
	ab = list(map(int, input().split()))
	ac = [ab[1] - ab[0]] + ab
	vec.append(ac)
vec.sort()
res = 0
for i in range(n):
	res += vec[i][1] * i + vec[i][2] * (n - 1 - i)
print(res)


def solve():
	n = int(input())
	a = list()
	b = list()
	a_append = a.append
	b_append = b.append
	split = str.split
	for i in range(n):
		(new_a, new_b) = map(int, split(input()))
		a_append(new_a)
		b_append(new_b)
	c = [(a[i] - b[i], i) for i in range(n)]
	c = sorted(c, key=lambda item: item[0], reverse=True)
	sum = 0
	for i in range(n):
		index = c[i][1]
		sum += a[index] * i + b[index] * (n - i - 1)
	print(sum)
solve()


n = int(input())
suma = 0
dif = []
for i in range(n):
	(a, b) = map(int, input().split())
	suma += b * (n - 1)
	dif.append(a - b)
dif = sorted(dif, reverse=True)
for j in range(n):
	suma += j * dif[j]
print(suma)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def primeFactors(n):
	pf = []
	while n % 2 == 0:
		pf.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			pf.append(int(i))
			n = n / i
	if n > 2:
		pf.append(int(n))
	return pf

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = 1
	for _ in range(tc):
		n = ri()
		z = []
		sa = 0
		sb = 0
		for i in range(n):
			(a, b) = ria()
			z.append(a - b)
			sa += a
			sb += b
		ans = n * sb - sa
		z = sorted(z)[::-1]
		for j in range(n):
			ans += (j + 1) * z[j]
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import os
import sys
from io import BytesIO, IOBase
_str = str
str = lambda x=b'': x if type(x) is bytes else _str(x).encode()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = lambda : sys.stdin.readline().rstrip('\r\n')
N = int(input())
A = []
B = []
for i in range(N):
	(a, b) = map(int, input().split())
	A.append(a)
	B.append(b)
Sub = []
for i in range(N):
	Sub.append((abs(A[i] - B[i]), i))
Sub.sort(reverse=True)
left = 0
right = N - 1
Ans = [0] * N
for i in range(N):
	if A[Sub[i][1]] >= B[Sub[i][1]]:
		Ans[left] = Sub[i][1]
		left += 1
	else:
		Ans[right] = Sub[i][1]
		right -= 1
Min = 0
for i in range(1, N + 1):
	Min += A[Ans[i - 1]] * (i - 1) + B[Ans[i - 1]] * (N - i)
print(Min)


n = int(input())
arr = []
for i in range(n):
	(a, b) = map(int, input().split())
	arr.append([a, b, a - b])
arr = sorted(arr, key=lambda item: item[2], reverse=True)
ans = 0
for i in range(n):
	ans += arr[i][0] * i + arr[i][1] * (n - i - 1)
print(ans)


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


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


import sys
input = sys.stdin.readline
n = int(input())
l = sorted([list(map(int, input().rstrip('\n').split(' '))) for i in range(n)], key=lambda x: x[1] - x[0])
print(sum((l[i][0] * i + l[i][1] * (n - i - 1) for i in range(n))))


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)


__version__ = '0.2'
__date__ = '2019-04-19'
import sys

def solve(n, ab):
	ordered_students = sorted(ab, key=lambda x: x[1] - x[0])
	return sum((a * j + b * (n - j - 1) for (j, (a, b)) in enumerate(ordered_students)))

def main(argv=None):
	n = int(input())
	ab = [list(map(int, input().split())) for student in range(n)]
	print(solve(n, ab))
	return 0
STATUS = main()
sys.exit(STATUS)


import sys, os, io
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from sys import stdin
import math

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def si():
	return input()

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

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

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = 1
for _ in range(t):
	n = ii()
	l = []
	a = []
	suma = 0
	sumb = 0
	for i in range(n):
		(x, y) = li()
		a.append([x, y])
		l.append([x - y, i])
		sumb += y
		suma += x
	l.sort(reverse=True)
	res = 0
	for i in range(n):
		A = a[l[i][1]][0]
		B = a[l[i][1]][1]
		res += A * i + B * (n - i - 1)
	print(res)


n = int(input())
v = [[int(x) for x in input().split()] for _ in range(n)]
ax_c = [(a - b, b * n - a) for (a, b) in v]
ax_c.sort(reverse=True)
s = sum((x[0] * (i + 1) + x[1] for (i, x) in enumerate(ax_c)))
print(s)


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


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 = [list(map(int, input().split())) for _ in range(n)]
a.sort(key=lambda x: x[1] - x[0])
ans = 0
for i in range(n):
	ans += a[i][0] * i + a[i][1] * (n - i - 1)
print(ans)


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().strip())
l = []

def p(ll):
	return ll[1] - ll[0]
for k in range(n):
	l.append(tuple(map(int, input().strip().split())))
l.sort(key=p)
s1 = 0
s2 = n - 1
s = 0
for tt in range(n):
	s = s + l[tt][0] * s1 + l[tt][1] * s2
	s2 -= 1
	s1 += 1
print(s)


from operator import itemgetter
n = int(input())
abi = [list(map(int, input().split())) for i in range(n)]
abi2 = [[abi[i][0], abi[i][1], abi[i][1] - abi[i][0]] for i in range(n)]
abi2.sort(key=itemgetter(2))
ans = 0
for i in range(n):
	ans += abi2[i][0] * i + abi2[i][1] * (n - 1 - i)
print(ans)


n = int(input())
p1 = 0
p2 = 0
l = []
s = 0
for k in range(1, n + 1):
	x = str(input())
	x = x.split(' ')
	x = [int(y) for y in x if y != '']
	p1 = x[0] + p1
	p2 = p2 + x[1]
	l.append(x[0] - x[1])
l.sort(reverse=True)
for k in range(1, n + 1):
	s = s + k * l[k - 1]
print(s + n * p2 - p1)


n = int(input())
li = []
li_p = []
for i in range(n):
	ly = input().split()
	ly.append(int(ly[0]) - int(ly[1]))
	li.append(ly)
li.sort(key=lambda x: x[2], reverse=True)
cost = 0
for i in range(n):
	cost += int(li[i][0]) * i + int(li[i][1]) * (n - i - 1)
print(cost)


from sys import stdin
n = int(input())
ab = []
lines = stdin.readlines()
for line in lines:
	(a, b) = map(int, line.split())
	ab.append((a, b))
ab.sort(key=lambda x: max(x[0], x[1]) - min(x[0], x[1]), reverse=True)
l = 0
r = n - 1
ans = 0
for (a, b) in ab:
	if a > b:
		ans += a * l + b * (n - l - 1)
		l += 1
	else:
		ans += a * r + b * (n - r - 1)
		r -= 1
print(ans)


n = int(input())
x = []
y = []
d = []
for _ in range(n):
	(xi, yi) = list(map(int, input().strip().split()))
	x.append(xi)
	y.append(yi)
	d.append(xi - yi)
constant_sum_x = -sum(x)
constant_sum_y = n * sum(y)
d.sort()
sm = 0
for i in range(n):
	sm += d[i] * (n - i)
print(constant_sum_x + constant_sum_y + sm)


from sys import stdin, stdout

def sortOdd(val):
	return val[0] - val[1]
(n, dis) = (int(stdin.readline()), 0)
a = [list(map(int, stdin.readline().split())) for i in range(n)]
a.sort(reverse=True, key=sortOdd)
for i in range(n):
	dis += a[i][0] * i + a[i][1] * (n - 1 - i)
print(dis)


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())
l = []
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	l += [(a, b, i)]

def keyf(ab):
	(a, b, i) = ab
	return -(a - b)
l.sort(key=keyf)
ans = 0
for i in range(n):
	ans += (n - 1) * l[i][1] + i * (l[i][0] - l[i][1])
print(ans)


import sys
input = sys.stdin.readline
n = int(input())
a = [None] * n
b = [None] * n
res = 0
for i in range(n):
	a[i] = list(map(int, input().split()))
	b[i] = a[i][0] - a[i][1]
rank = [index for (index, value) in sorted(list(enumerate(b)), key=lambda x: -x[1])]
for (i, index) in enumerate(rank):
	res += a[index][0] * i + a[index][1] * (n - i - 1)
print(res)


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)


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)


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = 1
	for _ in range(t):
		n = int(input())
		lis = []
		for i in range(n):
			(a, b) = inar()
			lis.append([a, b])
		ans = 0
		for i in range(n):
			ans += lis[i][1] * n - lis[i][0]
		take = []
		for i in range(n):
			take.append(lis[i][0] - lis[i][1])
		take.sort(reverse=True)
		for i in range(n):
			ans += take[i] * (i + 1)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def main():
	n = int(input())
	a = [0 for i in range(n)]
	b = [0 for i in range(n)]
	for i in range(n):
		(p, q) = input().split()
		a[i] = int(p)
		b[i] = int(q)
	q = []
	for i in range(n):
		q.append([a[i] - b[i], i])
	q.sort(reverse=True)
	val = 0
	for i in range(n):
		val += a[q[i][1]] * i + b[q[i][1]] * (n - i - 1)
	print(val)
main()


def cmp(a):
	return a[0] - a[1]
n = int(input())
v = []
for i in range(n):
	v.append(list(map(int, input().split())))
v = list(sorted(v, key=cmp, reverse=True))
s = 0
for i in range(n):
	s = s + v[i][0] * i + v[i][1] * (n - i - 1)
print(s)


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 math
import sys
from collections import defaultdict, Counter, deque, OrderedDict
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
from itertools import accumulate, groupby
N = int(input())
C = []
for i in range(N):
	(a, b) = ilele()
	C.append((a - b, a, b))
C.sort(reverse=True)
Ans = 0
for j in range(1, len(C) + 1):
	(c, a, b) = C[j - 1]
	Ans += a * (j - 1) + b * (N - j)
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 = list(map(int, input().split()))[0]
la = list()
lb = list()
for i in range(0, n):
	(a, b) = map(int, input().split())
	la += [(abs(a - b), a, b, i)]
la.sort()
lb.sort(reverse=True)
sa = set()
left = 0
right = n - 1
sums = 0
while len(la) > 0:
	g = la.pop()
	if int(g[1]) > int(g[2]):
		sums += int(g[1]) * left + int(g[2]) * (n - left - 1)
		left += 1
	else:
		sums += int(g[1]) * right + int(g[2]) * (n - right - 1)
		right -= 1
print(sums)


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


import math
n = int(input())
a = []
b = []
ans = 0
for i in range(n):
	(l, r) = map(int, input().split())
	ans += r * n - l
	a.append(l - r)
	b.append(i + 1)
a.sort(reverse=True)
for i in range(n):
	ans += a[i] * b[i]
print(ans)


from sys import stdin
input = stdin.readline
n = int(input())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append([a - b, a, b])
l.sort(reverse=True)
x = 0
for i in range(n, 0, -1):
	(j, a, b) = l[i - 1]
	x += i * j - a + b * n
print(x)


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


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


def main():
	n = int(input())
	ab = [tuple(map(int, input().split())) for _ in range(n)]
	students = sorted(range(n), key=lambda x: ab[x][1] - ab[x][0])
	total = 0
	for i in range(n):
		(a, b) = ab[students[i]]
		total += a * i + b * (n - i - 1)
	print(total)
main()


def solve(a):
	return a[0] - a[1]
n = int(input())
arr = []
for i in range(n):
	lista = list(map(int, input().split()))
	arr.append(lista)
ans = 0
for i in range(n):
	ans += arr[i][1] * n - arr[i][0]
arr.sort(reverse=True, key=solve)
for i in range(n):
	ans += (arr[i][0] - arr[i][1]) * (i + 1)
print(ans)


row = int(input())
data = [[0] * 2 for _ in range(row)]
for i in range(row):
	arr = input()
	arr = [int(x) for x in arr.split(' ')]
	for j in range(2):
		data[i][j] = arr[j]
data = sorted(data, key=lambda e: e[1] - e[0])
res = 0
for i in range(row):
	A = i
	B = row - i - 1
	res += data[i][0] * A + data[i][1] * B
print(res)


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


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
a.sort(key=lambda x: x[1] - x[0])
print(sum((a[i][0] * i + a[i][1] * (n - 1 - i) for i in range(n))))


N = int(input())
maps = []
for i in range(N):
	(a, b) = 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)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, OrderedDict
import math
import heapq

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
n = intIn()
arr = [0] * n
arr2 = []
di = {}
for i in range(1, n + 1):
	(a, b) = listIn()
	arr[i - 1] = [a, b, b - a]
order = sorted(arr, key=lambda x: x[2])
total = 0
for pos in range(1, n + 1):
	person = order[pos - 1]
	(a, b) = (person[0], person[1])
	total += a * (pos - 1) + b * (n - pos)
print(total)


import os
import sys
from operator import itemgetter
if 'DEBUG' in os.environ:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
n = int(input())
pairs = []
for _ in range(n):
	(a, b) = map(int, input().split())
	pairs.append((a, b))
pairs.sort(key=lambda p: p[1] - p[0])
ans = 0
for (i, p) in enumerate(pairs):
	ans += p[0] * i + p[1] * (n - (i + 1))
print(ans)


import math
import bisect
import heapq
from collections import defaultdict

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, x, y) = egcd(b % a, a)
		return (g, y - b // a * x, x)

def mulinv(b, n):
	(g, x, _) = egcd(b, n)
	if g == 1:
		return x % n
primes = []

def isprime(n):
	for d in range(2, int(math.sqrt(n)) + 1):
		if n % d == 0:
			return False
	return True

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	else:
		return int(input())
n = f()
a = []
b = []
c = []
sm = 0
for i in range(n):
	(x, y) = f(1)
	a.append(x)
	b.append(y)
	c.append(x - y)
ind = argsort(c)
for i in range(n):
	sm += (n - i - 1) * c[ind[i]] + b[i] * (n - 1)
print(sm)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
ab = []
for i in range(n):
	(a, b) = list(map(int, input().split()))
	ab.append((b - a, a, b))
ab = sorted(ab)
out = 0
for i in range(n):
	out += i * ab[i][1] + (n - i - 1) * ab[i][2]
print(out)


n = int(input())
ans = 0
c = []
for i in range(n):
	[a, b] = input().strip().split(' ')
	a = int(a)
	b = int(b)
	ans = ans + b * n - a
	c.append(b - a)
c = sorted(c)
tem = 0
for i in range(n):
	tem = tem + c[i] * (i + 1)
ans = ans - tem
print(ans)


from sys import stdin
input = stdin.readline
n = int(input())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort(key=lambda x: x[0] - x[1], reverse=True)
ans = 0
for i in range(n):
	ans += ab[i][0] * i + ab[i][1] * (n - 1 - i)
print(ans)


n = int(input())
v = []
d = []
for _ in range(n):
	v.append([int(item) for item in input().split()])
	d.append(v[-1][0] - v[-1][1])
d.sort()
j = n
i = 0
ans = 0
while j >= 1:
	ans += d[i] * j
	i += 1
	j -= 1
for i in range(n):
	ans += n * v[i][1] - v[i][0]
print(ans)


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


import os
from io import BytesIO, IOBase
BUFSIZE = 8192
import sys

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
L = []
for i in range(n):
	(a, b) = map(int, input().split())
	L.append([a, b])
L.sort(key=lambda x: -x[0] + x[1])
ans_1 = 0
ans_2 = 0
for i in range(n):
	ans_1 += L[i][0] * i + L[i][1] * (n - i - 1)
	ans_2 += L[i][0] * (n - i - 1) + L[i][1] * i
print(min(ans_1, ans_2))


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


n = int(input())
lst = []
for i in range(n):
	(a, b) = map(int, input().split())
	c = b - a
	lst.append((c, a, b))
lst.sort()
total = 0
for i in range(n):
	total += lst[i][1] * i + lst[i][2] * (n - i - 1)
print(total)


from sys import stdin
n = int(input())
a = sorted(list(([int(x) for x in i.split()] for i in stdin.readlines())), key=lambda x: x[0] - x[1], reverse=True)
ans = 0
for i in range(n):
	ans += a[i][0] * i + a[i][1] * (n - i - 1)
print(ans)


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))))


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


n = int(input())
m = []
for i in range(n):
	(a, b) = map(int, str.split(input(), ' '))
	m.append((a - b, a, b))
m.sort(reverse=True)
score = 0
for i in range(n):
	(_, a, b) = m[i]
	score += (i + 1) * (a - b) + b * n - a
print(score)


from sys import setcheckinterval, stdin, setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10 ** 6)
iin = lambda : int(stdin.readline())
lin = lambda : list(map(int, stdin.readline().split()))
n = iin()
a = [lin() for i in range(n)]
a1 = sorted(a, key=lambda x: x[1] - x[0])
ans = 0
for i in range(n):
	ans += a1[i][0] * i + a1[i][1] * (n - i - 1)
print(ans)


import sys

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		n = self.next_int()
		x = sorted([(self.next_int(), self.next_int()) for _ in range(0, n)], key=lambda o: o[1] - o[0])
		ans = 0
		for i in range(0, n):
			ans += x[i][0] * i + x[i][1] * (n - i - 1)
		print(ans)
Main().solve()


n = int(input())
iparray = []
sumab = 0
for i in range(n):
	ip = list(map(int, input().split()))
	iparray.append(ip[0] - ip[1])
	sumab += ip[1] * n - ip[0]
iparray.sort(reverse=True)
result = 0
for i in range(1, n + 1):
	result = result + i * iparray[i - 1]
final = result + sumab
print(final)


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input().strip('\n')
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
let = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(1):
	n = ii()
	l = [il() for i in range(n)]
	l.sort(reverse=True, key=lambda x: x[0] - x[1])
	ans = 0
	for i in range(n):
		ans += l[i][0] * i + l[i][1] * (n - i - 1)
	print(ans)


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


import os, sys, atexit
from io import BytesIO, StringIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
_OUTPUT_BUFFER = StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
n = int(input())
ans = 0
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	ans += b * n - a
	l.append(a - b)
l.sort(reverse=True)
for i in range(1, n + 1):
	ans += l[i - 1] * i
print(ans)


def argsort(seq):
	return sorted(range(len(seq)), key=seq.__getitem__)
n = int(input())
sta = []
geb = []
for i in range(n):
	a = list(map(int, input().split()))
	sta.append(a[1] - a[0])
	geb.append(a)
sor = argsort(sta)
left = 0
right = n - 1
ans = 0
for i in range(n):
	g = sor[i]
	ans += geb[g][0] * left + geb[g][1] * right
	left += 1
	right -= 1
print(ans)


n = int(input())
l = []
a = 0
b = 0
for i in range(n):
	l += [list(map(int, input().split()))]
	a += l[i][0]
	b += l[i][1]
l1 = [l[i][0] - l[i][1] for i in range(n)]
l1.sort(reverse=True)
s = 0
for i in range(1, n + 1):
	s += i * l1[i - 1]
s += n * b - a
print(s)


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


n = int(input())
s = []
bs = []
k = 0
for i in range(n):
	(a, b) = map(int, input().split())
	bs.append(b)
	s.append(a - b)
z = sorted(zip(s, bs))
s = [x for (x, y) in z][::-1]
bs = [y for (x, y) in z][::-1]
for i in range(n):
	k += s[i] * i + bs[i] * (n - 1)
print(k)


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


def compute(s, n):
	s_new = sorted(s, reverse=True)
	ret = 0
	for i in range(1, n + 1):
		ret = ret + i * s_new[i - 1]
	return ret

def main():
	n = int(input())
	s = []
	b_sum = 0
	a_sum = 0
	for i in range(n):
		(a, b) = input().split(' ')
		a = int(a)
		b = int(b)
		a_sum = a + a_sum
		b_sum = b + b_sum
		s.append(a - b)
	ret = compute(s, n)
	print(ret + b_sum * n - a_sum)
main()


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


n = int(input())
arr = []
for _ in range(n):
	arr.append(tuple(map(int, input().split())))
arr.sort(reverse=True, key=lambda x: x[0] - x[1])
result = 0
for i in range(len(arr)):
	result += arr[i][0] * i + arr[i][1] * (n - i - 1)
print(result)


import sys
input = sys.stdin.readline
N = int(input())
X = []
for _ in range(N):
	(a, b) = 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)


def main():
	n = int(input())
	arr = []
	for i in range(n):
		(x, y) = map(int, input().split())
		arr.append((x, y))
	ans = 0
	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()


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


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)


n = int(input())
l1 = []
l2 = []
l3 = []
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	l1.append(a)
	l2.append(b)
	l3.append(a - b)
l3.sort()
l3.reverse()
min1 = 0
for i in range(n):
	min1 += l3[i] * (i + 1) + l2[i] * n - l1[i]
print(min1)


n = int(input())
l = []
a1 = 0
b1 = 0
for i in range(n):
	(a, b) = map(int, input().split())
	a1 = a1 + a
	b1 = b1 + b
	l.append(a - b)
l.sort(reverse=True)
s = 0
x = [i for i in range(1, n + 1)]
i = 0
while len(l) > 0:
	if l[-1] <= 0:
		s = s + l.pop() * x.pop()
	else:
		break
for i in range(len(l)):
	s = s + l[i] * x[i]
print(n * b1 - a1 + s)


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)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
char = []
for i in range(n):
	char.append(tuple(map(int, input().split())))
ans = sum((char[i][1] * n - char[i][0] for i in range(n)))
char.sort(key=lambda x: x[0] - x[1], reverse=True)
for i in range(n):
	ans += (i + 1) * (char[i][0] - char[i][1])
print(ans)


import sys

def main():
	count = int(sys.stdin.readline().rstrip('\r\n'))
	items = []
	summ = 0
	i = 0
	while i < count:
		[l, r] = sys.stdin.readline().rstrip('\r\n').split()
		l = int(l)
		r = int(r)
		items.append([r - l, l, r])
		i += 1
	items.sort()
	i = 0
	while i < count:
		summ += items[i][1] * i + items[i][2] * (count - i - 1)
		i += 1
	print(summ)
main()


N = int(input())
items = []
for i in range(N):
	items.append(tuple(map(int, input().split())))
m = {}
for (i, item) in enumerate(items):
	(a, b) = item
	m[i] = a - b
m = sorted(m.items(), key=lambda x: -x[1])
ans = 0
i = 0
for (k, _) in m:
	(a, b) = items[k]
	ans += a * i + b * (N - i - 1)
	i += 1
print(ans)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
s = [tuple(map(int, input().split())) for i in range(n)]
s.sort(key=lambda x: x[1] - x[0])
print(sum((s[j][0] * j + s[j][1] * (n - 1 - j) for j in range(n))))


num = int(input())
pairs = []
for i in range(num):
	(a, b) = map(int, input().split())
	pairs.append((a, b, a - b))
pairs.sort(key=lambda x: -x[2])
dis = 0
for (i, pers) in enumerate(pairs):
	dis += pers[0] * i + pers[1] * (num - i - 1)
print(dis)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
n = int(inp())
ml = []
nl = []
for i in range(n):
	(a, b) = mp()
	ml.append((a, b))
	nl.append((a - b, i))
nl.sort(reverse=True)
ans = 0
for i in range(n):
	ans += nl[i][0] * (i + 1) + ml[nl[i][1]][1] * n - ml[nl[i][1]][0]
print(ans)


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


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())
nums = []
for i in range(n):
	nums.append(list(map(int, input().split())))
nums.sort(key=lambda x: x[1] - x[0])
ans = 0
for (idx, ele) in enumerate(nums):
	ans += idx * ele[0] + (n - idx - 1) * ele[1]
print(ans)


import sys
input = sys.stdin.readline
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda x: x[0] - x[1], reverse=True)
print(sum((x[0] * j + x[1] * (n - j - 1) for (j, x) in enumerate(ab))))


n = int(input())
weights = []
nums = []
for i in range(n):
	nums.append([int(x) for x in input().split()])
	weights.append(nums[i][0] - nums[i][1])
nums = [x[0] for x in sorted(zip(nums, weights), reverse=True, key=lambda x: x[1])]
cost = 0
for j in range(n):
	cost += nums[j][0] * j + nums[j][1] * (n - j - 1)
print(cost)


n = int(input())
l = []
ans = 0
for _ in range(n):
	(x, y) = map(int, input().split())
	ans += y * n - x
	l += [x - y]
l = sorted(l)
j = n
for each in l:
	ans += each * j
	j -= 1
print(ans)


import bisect
from itertools import accumulate, combinations, count
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
from sys import maxsize
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def SieveOfEratosthenes(n):
	prime = []
	primes = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if primes[p] == True:
			prime.append(p)
			for i in range(p * p, n + 1, p):
				primes[i] = False
		p += 1
	return prime

def primefactors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return sorted(fac)

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
n = int(input())
ans1 = []
ans2 = []
for i in range(n):
	(a, b) = map(int, input().split())
	ans1.append([b - a, a, b])
ans1.sort()
count = 0
for i in range(n):
	count += ans1[i][1] * i + ans1[i][2] * (n - i - 1)
print(count)


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)


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def computeLPSArray(pat, M, lps):
	len = 0
	lps[0]
	i = 1
	while i < M:
		if pat[i] == pat[len]:
			len += 1
			lps[i] = len
			i += 1
		elif len != 0:
			len = lps[len - 1]
		else:
			lps[i] = 0
			i += 1
m = 10 ** 9 + 7
t = 1
while t:
	t -= 1
	n = int(fastinput())
	x = []
	j = 1
	d = 0
	for _ in range(n):
		(a, b) = map(int, fastinput().split())
		d += b * n - a
		x.append(a - b)
		j += 1
	x.sort(reverse=True)
	j = 1
	for i in x:
		d += i * j
		j += 1
	print(d)


a = int(input())
k = 0
A = []
for i in range(a):
	(q, w) = map(int, input().split())
	k += w * a - q
	A.append(q - w)
A.sort(reverse=True)
for i in range(a):
	k += A[i] * (i + 1)
print(k)


a = int(input())
ab = []
m = []
for k in range(a):
	ab.append([int(k) for k in input().split()])
	m.append([ab[k][0] - ab[k][1], k])
m.sort()
m.reverse()
j = 0
sums = 0
for k in m:
	sums += ab[k[1]][0] * j + ab[k[1]][1] * (a - j - 1)
	j += 1
print(sums)


import sys
input = sys.stdin.readline
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda k: k[1] - k[0])
ans = 0
for i in range(n):
	(a, b) = ab[i]
	ans += a * i + b * (n - 1 - i)
print(ans)


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


import math

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def dva():
	(n, m) = map(int, input().split())
	b = [int(x) for x in input().split()]
	return (n, m, b)

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)
n = int(input())
a = []
b = []
a1 = []
a2 = []
for i in range(n):
	(x, y) = map(int, input().split())
	s = y - x
	a.append((s, x, y))
a = sorted(a)
su = 0
for i in range(n):
	(s1, s2) = (a[i][1], a[i][2])
	su += s1 * i + s2 * (n - i - 1)
print(su)


import sys
sys.setrecursionlimit(10 ** 5 + 1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
nb_students = RI()
line = []
for i in range(nb_students):
	(a, b) = RMI()
	line.append([b - a, a, b])
line.sort()
ans = 0
for i in range(nb_students):
	ans += i * line[i][1] + (nb_students - i - 1) * line[i][2]
print(ans)


class sol:

	def __init__(self, a, b):
		self.a = a
		self.b = b
		self.sub = a - b
n = int(input())
arr = []
for i in range(n):
	(a, b) = map(int, input().split())
	arr.append(sol(a, b))
arr = sorted(arr, key=lambda x: x.sub, reverse=True)
val = 0
for i in range(n):
	dis = arr[i].a * i + arr[i].b * (n - (i + 1))
	val += dis
print(val)


import sys

def input():
	return sys.stdin.readline()
n = int(input())
arr = []
for i in range(n):
	(a, b) = map(int, input().split())
	arr.append((a, b))
arr.sort(key=lambda x: x[1] - x[0])
answer = 0
for i in range(n):
	(a, b) = arr[i]
	answer += a * i + b * (n - i - 1)
print(answer)


(n, arr, ans) = (int(input()), list(), 0)
for i in range(n):
	[a, b] = [int(i) for i in input().split()]
	arr += [[b - a, a, b]]
arr = sorted(arr)
for i in range(n):
	ans += i * arr[i][1] + (n - i - 1) * arr[i][2]
print(ans)


n = int(input())
(sb, sa) = (0, 0)
c = []
for i in range(n):
	(i, j) = map(int, input().split())
	sb += j
	sa += i
	c.append(i - j)
c.sort()
c = c[::-1]
ans = sb * n - sa
for i in range(n):
	ans += c[i] * (i + 1)
print(ans)


import os
import sys
from bisect import bisect_right
from collections import Counter, defaultdict, deque
from heapq import *
from io import BytesIO, IOBase
from math import gcd, inf, sqrt, ceil

def lcm(a, b):
	return a * b // gcd(a, b)

def sieve():
	global spf, adj, MAXN
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(2, MAXN):
		if i * i > MAXN:
			break
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def getdistinctFactorization(n):
	global adj, spf, MAXN
	for i in range(1, n + 1):
		index = 1
		x = i
		if x != 1:
			adj[i].append(spf[x])
		x = x // spf[x]
		while x != 1:
			if adj[i][index - 1] != spf[x]:
				adj[i].append(spf[x])
				index += 1
			x = x // spf[x]

def printDivisors(n):
	i = 2
	z = [1, n]
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				z.append(i)
			else:
				z.append(i)
				z.append(n // i)
		i = i + 1
	return z

def create(n, x, f):
	pq = len(bin(n)[2:])
	if f == 0:
		tt = min
	else:
		tt = max
	dp = [[inf] * n for _ in range(pq)]
	dp[0] = x
	for i in range(1, pq):
		for j in range(n - (1 << i) + 1):
			dp[i][j] = tt(dp[i - 1][j], dp[i - 1][j + (1 << i - 1)])
	return dp

def enquiry(l, r, dp, f):
	if l > r:
		return inf if not f else -inf
	if f == 1:
		tt = max
	else:
		tt = min
	pq1 = len(bin(r - l + 1)[2:]) - 1
	return tt(dp[pq1][l], dp[pq1][r - (1 << pq1) + 1])

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	x = []
	for i in range(2, n + 1):
		if prime[i]:
			x.append(i)
	return x

def main():
	n = int(input())
	x = []
	y = 0
	for i in range(n):
		(xx, yy) = map(int, input().split())
		x.append((xx - yy, i))
		y += yy
	x.sort(reverse=True)
	an = 0
	for i in range(n):
		an += i * x[i][0]
	print(an + (n - 1) * y)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


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


n = int(input())
arr = []
for i in range(n):
	arr.append(list(map(int, input().split())))
	arr[i].append(arr[i][0] - arr[i][1])
	pass
arr = sorted(arr, key=lambda x: x[2], reverse=True)
sum = 0
for i in range(n):
	sum += i * arr[i][0] + (n - i - 1) * arr[i][1]
print(sum)


n = int(input())
chars = []
for i in range(n):
	(a, b) = map(int, input().strip().split())
	chars.append((i, a, b))
new_order = sorted(chars, key=lambda x: x[2] - x[1])
total_dis = 0
for (i, e) in enumerate(new_order):
	total_dis += chars[e[0]][1] * i + chars[e[0]][2] * (n - i - 1)
print(total_dis)

def total_order(p1, p2):
	pass


n = int(input())
arr = [[0] + [int(i) for i in input().split()] for i in range(n)]
for i in range(n):
	arr[i][0] = arr[i][1] - arr[i][2]
arr = sorted(arr)[::-1]
ans = 0
for i in range(n):
	ans += i * arr[i][1] + (n - i - 1) * arr[i][2]
print(ans)


n = int(input())
a = []
b = []
c = []
for i in range(n):
	d = list(map(int, input().split()))
	a.append(d[0])
	b.append(d[1])
	c.append(d[0] - d[1])
w = sum(a)
z = sum(b)
c.sort(reverse=True)
summ = 0
for i in range(len(c)):
	summ += c[i] * (i + 1)
t = summ + n * z - w
print(t)


from sys import stdin
input = stdin.readline
ans = 0
k = []
n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	ans += b * n - a
	k.append(a - b)
k.sort(reverse=True)
for i in range(n):
	ans += k[i] * (i + 1)
print(ans)


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)


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


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


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


n = int(input())
(a, b) = zip(*[map(int, input().split()) for _ in range(n)])
s = (n - 1) * sum(b)
d = sorted((a[i] - b[i] for i in range(n)))[::-1]
for i in range(n):
	s += d[i] * i
print(s)


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)


n = int(input())
a = []
k = 0
for i in range(n):
	(h, m) = list(map(int, input().split()))
	a.append(h - m)
	k += m
k *= n - 1
a.sort(reverse=True)
for i in range(n):
	k += i * a[i]
print(k)


t = int(input())
ss = []
for _ in range(0, t):
	(a, b) = map(int, input().split())
	ss.append([b - a, a, b])
ss = sorted(ss)
s = 0
for i in range(1, t + 1):
	s += ss[i - 1][1] * (i - 1) + ss[i - 1][2] * (t - i)
print(s)


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


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def getMat(n):
	return [getInts() for _ in range(n)]
MOD = 998244353

def solve():
	N = getInt()
	AB = getMat(N)
	AB.sort(key=lambda x: x[0] - x[1])
	ans = 0
	for j in range(N):
		ans += j * AB[j][1] + (N - j - 1) * AB[j][0]
	return ans
print(solve())


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


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)


from sys import stdin
input = stdin.readline
n = int(input())
arr = [tuple(map(int, input().split())) for _ in range(n)]
arr = [(a, b, a - b) for (a, b) in arr]
arr.sort(key=lambda x: (x[2], x[0]), reverse=True)
cnt = 0
for (i, t) in enumerate(arr):
	(a, b, _) = t
	cnt += i * a
	cnt += (n - i - 1) * b
print(cnt)


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
import math
import random
import re
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 9

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN():
	return list(numIN())

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def lgcd(a):
	g = a[0]
	for i in range(1, len(a)):
		g = math.gcd(g, a[i])
	return g

def ms(a):
	msf = -MAX
	meh = 0
	for i in range(len(a)):
		meh += a[i]
		if msf < meh:
			msf = meh
		if meh < 0:
			meh = 0
	return msf

def res(ans, t):
	print('Case #{}: {}'.format(t, ans))
n = int(input())
l = []
for _ in range(n):
	(a, b) = numIN()
	l.append([a, b, a - b])
l.sort(key=lambda x: x[2], reverse=True)
ans = 0
for i in range(n):
	ans += l[i][0] * i + l[i][1] * (n - i - 1)
print(ans)


import io, os
S = os.read(0, os.fstat(0).st_size)
py2 = round(0.5)

def readnumbers(s, zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	i = 0
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] != b'\r'[0]:
				A.append(numb)
				numb = 0
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(numb)
	return A

def mergesort(A, gt_cmp=None):
	if gt_cmp is None:
		gt_cmp = lambda i, j: i > j
	(B, n) = (A[:], len(A))
	for i in range(0, n - 1, 2):
		if gt_cmp(A[i], A[i ^ 1]):
			(A[i], A[i ^ 1]) = (A[i ^ 1], A[i])
	width = 2
	while width < n:
		for i in range(0, n, 2 * width):
			R1 = j = min(i + width, n)
			R2 = min(i + 2 * width, n)
			k = i
			while i < R1 and j < R2:
				if gt_cmp(A[i], A[j]):
					B[k] = A[j]
					j += 1
				else:
					B[k] = A[i]
					i += 1
				k += 1
			while i < R1:
				B[k] = A[i]
				k += 1
				i += 1
			while k < R2:
				B[k] = A[k]
				k += 1
		(A, B) = (B, A)
		width *= 2
	return A
inp = readnumbers(S)
n = inp[0]
a = list(range(n))
diff = [inp[2 * i + 2] - inp[2 * i + 1] for i in range(n)]
a = mergesort(a, gt_cmp=lambda i, j: diff[i] > diff[j])
s = 0
for i in range(n):
	s += inp[2 * a[i] + 1] * i + inp[2 * a[i] + 2] * (n - 1 - i)
print(s)


import sys
input = sys.stdin.readline
n = int(input())
AB = [list(map(int, input().split())) for i in range(n)]
AB.sort(key=lambda x: x[1] - x[0])
ANS = 0
for j in range(n):
	ANS += AB[j][0] * j + AB[j][1] * (n - j - 1)
print(ANS)


n = int(input())
a = []
for i in range(n):
	b = list(map(int, input().split()))
	a.append(b)
y = 0
x = 0
c = []
for i in a:
	x += i[0]
	y += i[1]
	c.append(i[0] - i[1])
c.sort(reverse=True)
p = n * y - x
s = 0
for j in range(n):
	s += c[j] * (j + 1)
print(s + p)


n = int(input())
people = list()
for i in range(n):
	people.append([int(el) for el in input().split()])
disbal = [(i, el[1] - el[0]) for (i, el) in enumerate(people)]
disbal = sorted(disbal, key=lambda x: x[1])
power = 0
for (cur, el) in enumerate(disbal):
	power += people[el[0]][0] * cur + people[el[0]][1] * (n - cur - 1)
print(power)


import sys
from math import log2, floor, ceil, sqrt
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
n = int(ri())
a = []
for i in range(n):
	(c, d) = Ri()
	a.append([c, d])
a.sort(key=lambda x: x[0] - x[1], reverse=True)
ans = 0
for i in range(len(a)):
	ans += (i + 1) * (a[i][0] - a[i][1]) + a[i][1] * n - a[i][0]
print(ans)


import sys
input = sys.stdin.readline
n = int(input())
AB = [tuple(map(int, input().split())) for _ in range(n)]
AB.sort(key=lambda x: x[1] - x[0])
ans = 0
for i in range(n):
	ans += AB[i][0] * i + AB[i][1] * (n - i - 1)
print(ans)


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))
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)


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
n = int(input().strip())
arr = []
for i in range(n):
	x = list(map(int, input().strip().split()))
	x.append(x[0] - x[1])
	arr.append(x)
arr = sorted(arr, key=lambda x: x[2], reverse=True)
total = 0
for i in range(n):
	j = i + 1
	ai = arr[i][0]
	bi = arr[i][1]
	total += ai * (j - 1) + bi * (n - j)
print(total)


n = int(input())
a = [tuple(map(int, input().split())) for _ in range(n)]
a.sort(key=lambda x: x[0] - x[1], reverse=True)
ret = 0
for i in range(1, n + 1):
	ret += a[i - 1][0] * (i - 1) + a[i - 1][1] * (n - i)
print(ret)


n = int(input())
ab = [tuple(map(int, input().split())) for i in range(n)]
a = [k[0] - k[1] for k in ab]
a.sort()
var = sum([a[i] * (n - i) for i in range(n)])
const = sum([k[1] * n - k[0] for k in ab])
print(var + const)


n = int(input())
a = []
b = []
for _ in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
s = 0
s -= sum(a)
s += sum(b) * n
array = [a[i] - b[i] for i in range(n)]
array.sort(reverse=True)
for j in range(1, n + 1):
	s += array[j - 1] * j
print(s)


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


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


n = int(input())
pos = []
neg = []
res = 0
for i in range(n):
	(a, b) = 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)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 1000000007
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
a = []
for i in range(n):
	a.append(value())
a.sort(key=lambda x: x[1] - x[0])
ans = 0
for i in range(n):
	(l, r) = a[i]
	ans += r * n - l + (i + 1) * (l - r)
print(ans)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from bisect import *
from math import sqrt, pi, ceil, log, inf, gcd
from itertools import permutations
from copy import deepcopy
from heapq import *

def main():
	n = int(input())
	a = []
	for i in range(n):
		a.append(tuple(map(int, input().split())))
	a.sort(key=lambda x: x[0] - x[1], reverse=True)
	ans = 0
	for i in range(n):
		ans += a[i][1] * n - a[i][0] + (i + 1) * (a[i][0] - a[i][1])
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def mp():
	return map(int, input().split())
n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
	(a[i], b[i]) = mp()
q = [0] * n
for i in range(n):
	q[i] = [b[i] - a[i], i]
q.sort()
e = 0
for i in range(n):
	j = q[i][1]
	e += a[j] * i + b[j] * (n - i - 1)
print(e)


import sys
input = sys.stdin.readline

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

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

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
a = []
ans = 0
for _ in range(n):
	(x, y) = int_array()
	ans += y * n - x
	a.append(x - y)
a.sort(reverse=1)
for (x, i) in enumerate(a, 1):
	ans += i * x
print(ans)


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


import collections as cc
import itertools as it
I = lambda : list(map(int, input().split()))
(n,) = I()
ans = 0
d = []
for i in range(n):
	(x, y) = I()
	ans += y * (n - 1)
	d.append(x - y)
d.sort(reverse=True)
for i in range(n):
	ans += d[i] * i
print(ans)


import os
import sys
from io import BytesIO, IOBase
ii = lambda : int(input())
lmii = lambda : list(map(int, input().split()))
slmii = lambda : sorted(map(int, input().split()))
li = lambda : list(input())
mii = lambda : map(int, input().split())
msi = lambda : map(str, input().split())

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def main():
	n = ii()
	lst = []
	for i in range(n):
		(a, b) = mii()
		lst.append([a, b])
	lst = sorted(lst, key=lambda x: -x[0] + x[1])
	ans = 0
	for i in range(len(lst)):
		ans += i * lst[i][0] + (n - i - 1) * lst[i][1]
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import copy
sys.setrecursionlimit(1000000)
N = int(input())
ab = [tuple(map(int, input().split())) for _ in range(N)]
ab = sorted(ab, key=lambda x: x[0] - x[1], reverse=True)
ans = 0
for i in range(N):
	left = ab[i][0] * i
	right = ab[i][1] * (N - i - 1)
	ans += left + right
print(ans)


n = int(input())
q1 = []
for i in range(n):
	(a1, b1) = map(int, input().split())
	q1.append((10 ** 8 - (a1 - b1), a1, b1))
q1.sort()
c = 0
for j in range(n):
	(a1, b1) = (q1[j][1], q1[j][2])
	c += a1 * j + b1 * (n - 1 - j)
print(c)


n = int(input())
a = [0] * n
b = [0] * n
c = [0] * n
for i in range(0, n):
	(a[i], b[i]) = map(int, input().split())
	c[i] = b[i] - a[i]
c = sorted(c)
s = 0
for i in range(0, n):
	s += c[i] * (n - 1 - i)
print(s + (n - 1) * sum(a))


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])
t = 0
for (i, (l, r)) in enumerate(data):
	t += l * i + r * (lines - i - 1)
print(t)


n = int(input())
l = []
for i in range(n):
	k = [int(x) for x in input().split()]
	l.append([k[0], k[1], k[0] - k[1]])
l = sorted(l, key=lambda r: r[2], reverse=True)
s = 0
for i in range(n):
	s += l[i][0] * i + l[i][1] * (n - 1 - i)
print(s)


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


import sys
n = int(sys.stdin.readline().strip())
Q = [0] * n
for i in range(0, n):
	line = sys.stdin.readline().strip().split()
	a = int(line[0])
	b = int(line[1])
	Q[i] = [a, b, b - a]
Q.sort(key=lambda x: x[2])
s = 0
for i in range(0, n):
	s = s + Q[i][0] * i + Q[i][1] * (n - 1 - i)
print(s)


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


def check(a, i, j, n):
	return a[i][0] * (j - 1) + a[i][1] * (n - j)
n = int(input())
a = [0]
b = []
for i in range(1, n + 1):
	a.append(list(map(int, input().split())))
	b.append([a[-1][0] - a[-1][1], i])
b.sort(reverse=True)
ans = 0
for j in range(1, n + 1):
	ans += check(a, b[j - 1][1], j, n)
print(ans)


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


from sys import stdin
inp = stdin.readline
(n, s) = (int(inp()), 0)
arr = sorted([[a[0] - a[1], a[0], a[1]] for i in range(n) for a in [[int(i) for i in inp().split()]]])
for i in range(n):
	s += arr[i][2] * i + arr[i][1] * (n - i - 1)
print(s)


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