A conglomerate consists of $n$ companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.

But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.

To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.

Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.


-----Input-----

The first line contains a single integer $n$ — the number of companies in the conglomerate ($1 \le n \le 2 \cdot 10^5$). Each of the next $n$ lines describes a company. 

A company description start with an integer $m_i$ — the number of its employees ($1 \le m_i \le 2 \cdot 10^5$). Then $m_i$ integers follow: the salaries of the employees. All salaries are positive and do not exceed $10^9$. 

The total number of employees in all companies does not exceed $2 \cdot 10^5$. 


-----Output-----

Output a single integer — the minimal total increase of all employees that allows to merge all companies.


-----Example-----
Input
3
2 4 3
2 2 1
3 1 1 1

Output
13



-----Note-----

One of the optimal merging strategies is the following. First increase all salaries in the second company by $2$, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries $[4, 3, 4, 3]$ and $[1, 1, 1]$. To merge them, increase the salaries in the second of those by $3$. The total increase is $2 + 2 + 3 + 3 + 3 = 13$.

Source: codeforces | Difficulty: EASY
Tags: Greedy algorithms


Solutions:
n = int(input())
m = 0
max_ = float('-inf')
max_i = 0
r = []
for i in range(n):
	tmp = [int(x) for x in input().split()]
	tmp = tmp[1:]
	r.append(tmp)
	if max(tmp) > max_:
		max_ = max(tmp)
		max_i = i
for (i, val) in enumerate(r):
	if i != max_i:
		m = m + (max_ - max(val)) * len(val)
print(m)


n = int(input())
a = []
(maxe, counter) = (0, 0)
for i in range(n):
	a.append(list(map(int, input().split())))
for i in a:
	if maxe < max(i[1:]):
		maxe = max(i[1:])
for i in a:
	counter += i[0] * (maxe - max(i[1:]))
print(counter)


n = int(input())
LL = []
m = 0
for i in range(n):
	l = list(map(int, input().split()))[1:]
	LL.append(l)
	m = max(m, max(l))
s = 0
for i in LL:
	s += len(i) * (m - max(i))
print(s)


def func(inpstr):
	return inpstr[0]
N = int(input())
arr = []
money = 0
for i in range(N):
	a = [int(i) for i in input().split()]
	arr.append([max(a[1:])] + [a[0]])
arr.sort(key=func)
while len(arr) != 1:
	p = arr[-2][1]
	m = arr[-1][0] - arr[-2][0]
	money += m * p
	arr[-1][1] += p
	arr.pop(-2)
print(money)


s = 0
a = int(input())
z = []
for i in range(a):
	gh = list(map(int, input().split()))
	gh = [gh[0], max(gh[1:])]
	if gh[1] > s:
		s = gh[1]
	z += [gh]
print(sum((z[i][0] * (s - z[i][1]) for i in range(a) if z[i][1] < s)))


n = int(input())
c = []
ms = []
for i in range(n):
	x = list(map(int, input().split()))
	count = x[0]
	m = max(x[1:])
	c += [count]
	ms += [m]
new = max(ms)
total = 0
for i in range(n):
	count = c[i]
	diff = new - ms[i]
	total += diff * count
print(total)


import sys
input = sys.stdin.readline
pp = 12
n = int(input())
m = -10
ans = 0
sum_of_tedad = 0
for _ in range(n):
	f = list(map(int, input().split()))
	a = f[0]
	sum_of_tedad += a
	del f[0]
	k = max(f)
	ans -= k * a
	m = max(m, k)
ans += m * sum_of_tedad
print(ans)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
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
start_time = time.time()

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

def get_ints():
	return map(int, sys.stdin.readline().split())
T = int(input())
ar = []
maxx = 0
for _ in range(T):
	arr = list(get_ints())
	m = max(arr[1:])
	ar.append([m, arr[0]])
	maxx = max(m, maxx)
ans = 0
for i in range(T):
	ans += (maxx - ar[i][0]) * ar[i][1]
print(ans)


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


n = int(input())
(k, maxi, masa, masm) = (0, 0, [], [])
for i in range(n):
	(a, *b) = [int(b) for b in input().split()]
	if maxi < max(b):
		maxi = max(b)
	masa.append(a)
	masm.append(max(b))
for i in range(n):
	k += (maxi - masm[i]) * masa[i]
print(k)


n = int(input())
M = []
A = []
for i in range(n):
	a = list(map(int, input().split(' ')))
	M.append(a[0])
	A.append(max(a[1:]))
temp = max(A)
ans = 0
for i in range(n):
	ans += abs(temp - A[i]) * M[i]
print(ans)


companies_numb = int(input())
max_wages = []
empl_numb = []
answer = 0
for comp in range(companies_numb):
	company = list(map(int, input().split()))
	max_wages.append(max(company[1:]))
	empl_numb.append(len(company) - 1)
total_wage = max(max_wages)
for numb in range(len(max_wages)):
	answer += (total_wage - max_wages[numb]) * empl_numb[numb]
print(answer)


n = int(input())
m = n * [0]
for i in range(n):
	en = list(map(int, input().split()))
	m[i] = en[1:]
max_line_index = 0
maximum = m[0][0]
for i in range(n):
	for j in range(len(m[i])):
		if m[i][j] > maximum:
			maximum = m[i][j]
			max_line_index = i
increase = 0
for i in range(n):
	if i != max_line_index:
		cur_max = max(m[i])
		increase += (maximum - cur_max) * len(m[i])
print(increase)


n = int(input())
ct = [0] * n
mx = [0] * n
for i in range(n):
	a = list(map(int, input().split()))
	ct[i] = a[0]
	mx[i] = max(a[1:])
ans = 0
rf = max(mx)
for i in range(n):
	ans = ans + (rf - mx[i]) * ct[i]
print(ans)


def get_max(arr):
	max = -1
	for i in range(len(arr)):
		if i > 0 and arr[i] > max:
			max = arr[i]
	return max

def main():
	n = int(input())
	companies = [[] for i in range(n)]
	max_arr = [0] * n
	for i in range(n):
		companies[i] = [int(x) for x in input().split()]
		max_arr[i] = get_max(companies[i])
	max_val = max(max_arr)
	total_cost = 0
	for i in range(len(max_arr)):
		total_cost += companies[i][0] * (max_val - max_arr[i])
	print(total_cost)
main()


t = int(input())
c = []
d = []
m = sum = 0
for i in range(0, t):
	h = list(map(int, input().split()))
	c.append(h[0])
	d.append(max(h[1:]))
m = max(d)
for i in range(0, t):
	sum = sum + (m - d[i]) * c[i]
print(sum)


n = int(input())
maior = 0
m = 0
banco = 0
for i in range(n):
	a = input().split('\n')
	a = a[0].split(' ')
	s = int(a[0])
	a.pop(0)
	lista = []
	for j in a:
		lista.append(int(j))
	lista.sort(reverse=True)
	if i == 0:
		maior = lista[0]
	if maior > lista[0]:
		banco += (maior - lista[0]) * s
	elif maior < lista[0]:
		banco += (lista[0] - maior) * m
		maior = lista[0]
	m += s
print(banco)


from sys import stdin, stdout
input = stdin.readline
n = int(input())
(a, s, t) = ([], 0, [])
for i in range(n):
	b = list(map(int, input().split()))
	a.append(max(b[1:]))
	t.append(b[0])
boro = max(a)
for i in range(n):
	s += (boro - a[i]) * t[i]
stdout.write(str(s))


com = int(input())
zar = []
y = 0
for i in range(com):
	line = [int(x) for x in input().split(' ')]
	zar.append(line)
for i in range(com):
	l = 0
	for j in range(1, zar[i][0] + 1):
		if l < zar[i][j]:
			l = zar[i][j]
	zar[i][1] = l
for i in range(com - 1):
	u = 0
	if zar[0][1] > zar[i + 1][1]:
		u = zar[0][1] - zar[i + 1][1]
		y += u * zar[i + 1][0]
	else:
		u = zar[i + 1][1] - zar[0][1]
		y += u * zar[0][0]
		zar[0][1] = zar[i + 1][1]
	zar[0][0] += zar[i + 1][0]
print(y)


n = int(input())
m = 0
co = []
r = []
for i in range(0, n, 1):
	a = list(map(int, input().split(' ')))
	co.append(a[0])
	r.append(max(a[1:]))
res = max(r)
tot = 0
for i in range(n):
	if r[i] != res:
		x = res - r[i]
		y = co[i] * x
		tot += y
print(tot)


from collections import deque as de
import math
from sys import stdin, stdout
import re
from collections import Counter as cnt
from functools import reduce
from itertools import groupby as gb
from bisect import bisect_left as bl, bisect_right as br

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

class My_stack:

	def __init__(self):
		self.data = []

	def my_push(self, x):
		return self.data.append(x)

	def my_pop(self):
		return self.data.pop()

	def my_peak(self):
		return self.data[-1]

	def my_contains(self, x):
		return self.data.count(x)

	def my_show_all(self):
		return self.data

	def isEmpty(self):
		return len(self.data) == 0
arrStack = My_stack()

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binarytodecimal(n):
	return int(n, 2)

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 get_prime_factors(number):
	prime_factors = []
	while number % 2 == 0:
		prime_factors.append(2)
		number = number / 2
	for i in range(3, int(math.sqrt(number)) + 1, 2):
		while number % i == 0:
			prime_factors.append(int(i))
			number = number / i
	if number > 2:
		prime_factors.append(int(number))
	return prime_factors

def get_frequency(list):
	dic = {}
	for ele in list:
		if ele in dic:
			dic[ele] += 1
		else:
			dic[ele] = 1
	return dic

def Log2(x):
	return math.log10(x) / math.log10(2)

def getProduct(n):
	product = 1
	while n != 0:
		product = product * (n % 10)
		n = n // 10
	return product

def lcm(x, y):
	lcm = x * y // math.gcd(x, y)
	return lcm

def isPowerOfTwo(n):
	return math.ceil(Log2(n)) == math.floor(Log2(n))

def checkisap(list):
	d = list[1] - list[0]
	for i in range(2, len(list)):
		temp = list[i] - list[i - 1]
		if temp != d:
			return False
	return True

def ceildiv(x, y):
	return (x + y - 1) // y

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

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

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

def si():
	return list(map(int, input()))

def indict():
	dic = {}
	for (index, value) in enumerate(input().split()):
		dic[int(value)] = int(index) + 1
	return dic

def frqdict():
	dic = {}
	for (index, value) in enumerate(input().split()):
		if int(value) not in dic:
			dic[int(value)] = 1
		else:
			dic[int(value)] += 1
	return dic
n = ii()
temp = []
mx = 0
while n:
	n -= 1
	row = li()
	del row[0]
	temp.append(row)
	maximum = max(row)
	if maximum > mx:
		mx = maximum
ans = 0
for row in temp:
	tt = max(row)
	if tt != mx:
		ans += len(row) * (mx - tt)
print(ans)


numeroEmpleados = list()
maxi = list()
for _ in range(int(input())):
	actual = list(map(int, input().split()))
	maxi.append(max(actual[1:]))
	numeroEmpleados.append(actual[0])
res = 0
maxGlobal = max(maxi)
for (num, m) in zip(numeroEmpleados, maxi):
	res += (maxGlobal - m) * num
print(res)


n = int(input())
b = [int(x) for x in input().split()]
q = b[0]
p = max(b[1:])
total = 0
for i in range(n - 1):
	a = [int(x) for x in input().split()]
	c = a[0]
	m = max(a[1:])
	if p > m:
		total += c * (p - m)
	elif m > p:
		total += q * (m - p)
		p = m
	q += c
print(total)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
N = 5 * 10 ** 5 + 7
d = []
mx = 0
for i in range(I()):
	a = L()
	m = max(a[1:])
	mx = max(mx, m)
	d.append([m, a[0]])
ans = 0
for i in d:
	ans += i[1] * (mx - i[0])
print(ans)


n = int(input())
info = []
for i in range(n):
	info.append(list(map(int, input().split())))
maxval = 0
maxes = []
for i in range(n):
	comax = 0
	for j in range(1, len(info[i])):
		if comax < info[i][j]:
			comax = info[i][j]
		if maxval < info[i][j]:
			maxval = info[i][j]
	maxes.append(comax)
increase = 0
for i in range(n):
	increase += info[i][0] * (maxval - maxes[i])
print(increase)


n = int(input())
maxi = []
cost = ma = 0
for _ in range(n):
	arr = [int(i) for i in input().split()]
	maximum = max(arr[1:])
	maxi.append((maximum, arr[0]))
	ma = max(maximum, ma)
for j in range(n):
	cost += (ma - maxi[j][0]) * maxi[j][1]
print(cost)


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


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	m = []
	for i in range(n):
		m.append(list(map(int, input().split()))[1:])
	for i in range(n):
		m[i].sort()
		m[i].reverse()
	m.sort()
	m.reverse()
	ans = 0
	currlen = 0
	for i in range(n - 1, 0, -1):
		currlen += len(m[i])
		if m[i][0] < m[i - 1][0]:
			ans += currlen * (-m[i][0] + m[i - 1][0])
	print(ans)
	return
main()


def f():
	v = list(map(int, input().split()))
	return (v[0], max(v[1:]))
n = int(input())
a = [f() for _ in range(n)]
x = 0
for v in a:
	x = max(x, v[1])
s = 0
for v in a:
	s += (x - v[1]) * v[0]
print(s)


def main():
	n = int(input())
	store = []
	true_max = 0
	for _ in range(n):
		arr = input().split()
		count = int(arr[0])
		tot = 0
		maxi = 0
		for y in range(1, count + 1):
			maxi = max(maxi, int(arr[y]))
			tot += int(arr[y])
		true_max = max(maxi, true_max)
		store.append((count, tot, maxi))
	tot = 0
	for x in store:
		tot += (true_max - x[2]) * x[0]
	print(tot)
main()


n = int(input())
(l, q, r) = ([0] * n, [0] * n, 0)
for i in range(n):
	a = list(map(int, input().split()))
	l[i] = a[0]
	a.remove(l[i])
	q[i] = max(a)
z = max(q)
for i in range(len(q)):
	r += (z - q[i]) * l[i]
print(r)


n = int(input())
tot = []
for i in range(n):
	x = [int(x) for x in input().split()]
	maxim = max(x[1:])
	N = x[0]
	tot.append((maxim, N))
tot.sort()
counter = 0
summa = tot[0][1]
for i in range(1, len(tot)):
	counter += (tot[i][0] - tot[i - 1][0]) * summa
	summa += tot[i][1]
print(counter)


import sys
input = sys.stdin.readline
N = int(input())
tot = []
for i in range(N):
	x = list(map(int, input().split()))
	maxim = max(x[1:])
	n = x[0]
	tot.append((maxim, n))
tot.sort()
counter = 0
summa = tot[0][1]
for i in range(1, len(tot)):
	counter += (tot[i][0] - tot[i - 1][0]) * summa
	summa += tot[i][1]
print(counter)


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial
from collections import defaultdict, Counter, deque
from itertools import permutations
maxx = float('inf')
I = lambda : int(sys.stdin.buffer.readline())
tup = lambda : map(int, sys.stdin.buffer.readline().split())
lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda : sys.stdin.readline().replace('\n', '').strip()

def grid(r, c):
	return [lint() for i in range(r)]
stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
localsys = 0
start_time = time.time()
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
n = I()
ar = [lint()[1:] for _ in range(n)]
m = [max(i) for i in ar]
c = max(m)
ans = 0
i = 0
st = []
while i < n:
	ans += len(ar[i]) * (c - m[i])
	i += 1
print(ans)
if localsys:
	print('\n\nTime Elased :', time.time() - start_time, 'seconds')


all_companies = []
max_salary = 0
n = int(input())
for _ in range(n):
	m = tuple((int(i) for i in input().split()))
	m = [m[0], max(m[1:])]
	max_salary = m[1] if m[1] > max_salary else max_salary
	all_companies.append(m)
total_increase = 0
for ind in range(n):
	if all_companies[ind][1] != max_salary:
		total_increase += (max_salary - all_companies[ind][1]) * all_companies[ind][0]
print(total_increase)


t = int(input())
lst = []
for _ in range(0, t):
	a = [int(i) for i in input().split()]
	lst.append((max(a[1:]), a[0]))
lst = sorted(lst)
maxx = lst[0][0]
mem = lst[0][1]
ss = 0
for i in range(1, len(lst)):
	if lst[i][0] == maxx:
		mem += lst[i][1]
		continue
	maxx = lst[i][0]
	ss += (lst[i][0] - lst[i - 1][0]) * mem
	mem += lst[i][1]
print(ss)


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


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	d = {}
	cur_max = -100
	for i in range(n):
		(m, *a) = map(int, input().split())
		d[i] = [m, a]
		cur_max = max(cur_max, max(a))
	answer = 0
	for k in d:
		answer += (cur_max - max(d[k][1])) * d[k][0]
	print(answer)
	return
main()


import math
x = int(input())
sums = 0
max_ele = []
max_kol = []
for i in range(x):
	l1 = list(map(int, input().split()))
	max_ele += [max(l1[1:])]
	max_kol += [l1[0]]
max_el = max(max_ele)
indexik = max_ele.index(max_el)
del max_kol[indexik]
del max_ele[indexik]
for i in range(x - 1):
	sums += max_kol[i] * (max_el - max_ele[i])
print(sums)


def odin(a):
	for i in range(len(a) - 1):
		if a[i] != a[i + 1]:
			return 0
	return 1
n = int(input())
k = 0
sm = []
m = []
for i in range(n):
	a = [int(i) for i in input().split()]
	m.append(a[0])
	a.remove(a[0])
	sm.append(max(a))
mx = max(sm)
for i in range(len(sm)):
	k += (mx - sm[i]) * m[i]
print(k)


n = int(input())
ans = []
mx = 0
for i in range(n):
	a = [int(i) for i in input().split()]
	z = a.pop(0)
	mx = max(mx, max(a))
	ans.append((max(a), z))
cnt = 0
for i in ans:
	cnt += (mx - i[0]) * i[1]
print(cnt)


import sys
input = sys.stdin.readline

def Input():
	return list(map(int, input().split()))
n = int(input())
sal = []
size = []
for i in range(n):
	now = Input()
	size.append(now.pop(0))
	sal.append(max(now))
bigMax = max(sal)
ans = 0
for i in range(n):
	ans += (bigMax - sal[i]) * size[i]
print(ans)


def max(l):
	m = l[0]
	for i in range(0, len(l)):
		if l[i] > m:
			m = l[i]
	return m

def new_merge(p, q):
	(a, b, k1) = p
	(c, d, k2) = q
	if b > d:
		k = (b - d) * c + k1 + k2
		x = a + c
		return (x, b, k)
	else:
		k = (d - b) * a + k1 + k2
		x = a + c
		return (x, d, k)

def merge(l):
	if len(l) == 1:
		print(l[0][2])
	else:
		n = len(l)
		l2 = []
		if n % 2 == 0:
			for i in range(0, n, 2):
				l2.append(new_merge(l[i], l[i + 1]))
			return merge(l2)
		else:
			for i in range(0, n - 1, 2):
				l2.append(new_merge(l[i], l[i + 1]))
			l2.append(l[n - 1])
			return merge(l2)
n = int(input())
l2 = []
for i in range(0, n):
	l = list(map(int, input().split()))
	x = l[0]
	l.remove(x)
	m = max(l)
	l2.append((x, m, 0))
merge(l2)


n = int(input())
emp = [[0, 0] for i in range(n)]
mx = float('-inf')
for i in range(n):
	arr = list(map(int, input().split()))
	emp[i][0] = arr[0]
	emp[i][1] = max(arr[1:])
	mx = max(mx, emp[i][1])
ans = 0
for i in range(n):
	if emp[i][0] != mx:
		ans += emp[i][0] * (mx - emp[i][1])
print(ans)


n = int(input())
A = []
for i in range(n):
	B = list(map(int, input().split()))
	ni = B[0]
	B.remove(ni)
	ai = max(B)
	T = [ni, ai]
	A.append(T)
a = A[0][1]
idx = 0
for i in range(n):
	if A[i][1] > a:
		a = A[i][1]
		idx = i
(A[0], A[idx]) = (A[idx], A[0])
p = 0
q = 0
for i in range(1, n):
	p += A[i][0]
	q += A[i][0] * A[i][1]
ans = A[0][1] * p - q
print(ans)


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

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

def li():
	return list(mi())
n = ii()
c = []
for i in range(n):
	a = li()[1:]
	c.append(a)
mx = max((max(e) for e in c))
ans = sum(((mx - max(e)) * len(e) for e in c))
print(ans)


t = int(input())
n = t
m = []
while t > 0:
	l = list(map(int, input().split()))
	x = max(l[1:])
	m.append((x, l[0]))
	t -= 1
m.sort(reverse=True, key=lambda x: x[0])
x = m[0][0]
m.remove(m[0])
s = 0
for i in range(n - 1):
	d = x - m[i][0]
	s += d * m[i][1]
print(s)


n = int(input())
a = [list(map(int, input().split()))[1:] for q in range(n)]
max1 = [max(q) for q in a]
max_max = max(max1)
answer = 0
for q in range(n):
	answer += (max_max - max1[q]) * len(a[q])
print(answer)


from collections import defaultdict
import bisect
from itertools import accumulate
import os
import sys
import math
from io import BytesIO, IOBase
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')
n = int(input())
ans = []
for i in range(n):
	a = list(map(int, input().split()))
	ans.append([max(a[1:]), a[0]])
ans.sort(reverse=True)
count = 0
for i in range(1, len(ans)):
	count += (ans[0][0] - ans[i][0]) * ans[i][1]
print(count)


n = int(input())
c = []
w = 0
while w < n:
	c.append(list(map(int, input().split())))
	m = c[w][0]
	c[w] = [max(c[w][1:m + 1]), m]
	w += 1
w = 0
prise = c[w][0]
count = c[w][1]
sum = 0
while w < n - 1:
	w += 1
	f = c[w][0]
	l = c[w][1]
	if f >= prise:
		sum += count * (f - prise)
		prise = f
	else:
		sum += l * (prise - f)
	count += l
print(sum)


from sys import stdin
n = int(input())
em = [0] * n
mx = [0] * n
for k in range(n):
	ar = [int(x) for x in stdin.readline().split()]
	em[k] = ar[0]
	mx[k] = max(ar[1:])
ans = 0
maxx = max(mx)
for k in range(n):
	ans += em[k] * (maxx - mx[k])
print(ans)


from sys import stdin, stdout

def main():
	n = int(stdin.readline())
	a = []
	for _ in range(n):
		l = list(map(int, stdin.readline().split()))
		(length, m) = (l[0], max(l[1:]))
		a.append([length, m])
		del l
	a.sort(key=lambda x: x[1])
	prev = a[0][0]
	ans = 0
	for i in range(1, n):
		ans += prev * (a[i][1] - a[i - 1][1])
		prev += a[i][0]
	stdout.write('%d\n' % ans)
main()


n = int(input())
a = []
c = []
y = -1
m = -1
for i in range(0, n):
	b = list(map(int, input().strip().split()))
	y = -1
	for j in range(1, len(b)):
		if b[j] > y:
			y = b[j]
		if b[j] > m:
			m = b[j]
	a.append(y)
	c.append(b[0])
p = 0
for i in range(0, n):
	p = p + (m - a[i]) * c[i]
print(p)


t = int(input())
arr = []
data = []
maxi = 0
for i in range(t):
	arr1 = list(map(int, input().split()))
	n = arr1[0]
	em = arr1[1:]
	em.sort()
	arr.append(n)
	data.append(em[-1])
	if maxi < em[-1]:
		maxi = em[-1]
inc = 0
for i in range(t):
	dif = maxi - data[i]
	if dif != 0:
		inc = inc + dif * arr[i]
print(inc)


from sys import stdin
max_val = int(10000000000000.0)
min_val = int(-10000000000000.0)

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

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

def read_str():
	return input()

def read_strs():
	return [x for x in stdin.readline().split()]
nb_companies = read_int()
employees = []
maxs = 0
for i in range(nb_companies):
	values = read_ints()
	nb_employee = values[0]
	salaries = values[1:]
	max_s = max(salaries)
	maxs = max(max_s, maxs)
	employees.append((nb_employee, max_s))
adjustment = 0
for i in employees:
	adjustment += i[0] * (maxs - i[1])
print(adjustment)


i = int(input())
l = []
for _ in range(i):
	k = list(map(int, input().strip().split()))
	l.append(k)
l.sort(key=lambda x: max(x[1:]), reverse=True)
k = max(l[0][1:])
sums = 0
for i in range(1, len(l)):
	j = (k - max(l[i][1:])) * (len(l[i]) - 1)
	sums += j
print(sums)


from operator import *
n = int(input())
company = []
g_max = 0
dic = {}
for i in range(n):
	t = list(map(int, input().split()))
	company.append(t)
	g_max = max(g_max, max(t[1:]))
for i in range(n):
	dic[i] = company[i][0] * (g_max - max(company[i][1:]))
itr = sorted(dic.items(), key=itemgetter(1))
res = 0
for i in itr:
	if i[1] != 0:
		res += i[1]
print(res)


n = int(input())
company = []
result = 0
for i in range(n):
	m = [int(x) for x in input().split(' ')]
	m.pop(0)
	company.append(m)
maxx = company[0][0]
for i in company:
	for j in i:
		if j > maxx:
			maxx = j
for i in range(n):
	if max(company[i]) < maxx:
		curent_max = max(company[i])
		for j in range(len(company[i])):
			company[i][j] += maxx - curent_max
			result += maxx - curent_max
print(result)


import sys

def fi():
	return sys.stdin.readline()
n = int(fi())
d = []
p = []
max1 = 0
for i in range(n):
	l = list(map(int, fi().split()))
	a = max(l[1:])
	l.append(a)
	max1 = max(a, max1)
	d.append(l)
c = 0
for i in range(n):
	if d[i][-1] != max1:
		a = max1 - d[i][-1]
		c += a * d[i][0]
print(c)


n = int(input())
comps = []
sizes = []
large = []
for i in range(n):
	a = list(map(int, input().split()))
	sizes.append(a[0])
	comps.append(a[1:])
big = 0
for i in range(n):
	a = max(comps[i])
	large.append(a)
	big = max(big, a)
inc = 0
for i in range(n):
	inc += sizes[i] * (big - large[i])
print(inc)


from sys import stdin
from heapq import heappush, heappop
n = int(input())
arr = []
maxi = 0
for i in range(n):
	(m, *tmp) = list(map(int, stdin.readline().split()))
	tmp = max(tmp)
	maxi = max(maxi, tmp)
	arr.append([m, tmp])
ans = 0
for i in range(n):
	ans += (maxi - arr[i][1]) * arr[i][0]
print(ans)


n = int(input())
em = [0] * n
mx = [0] * n
for k in range(n):
	ar = list(map(int, input().split()))
	em[k] = ar[0]
	mx[k] = max(ar[1:])
ans = 0
maxx = max(mx)
for k in range(n):
	ans += em[k] * (maxx - mx[k])
print(ans)


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


N = int(input())
rec = []
ma = 0
for i in range(N):
	M = list(map(int, input().split()))
	rec.append(M[1:])
	ma = max(ma, max(M[1:]))
num = 0
for i in range(N):
	m = max(rec[i])
	num += (ma - m) * len(rec[i])
print(num)


z = []
k = []
x = int(input())
for _ in range(x):
	s = [int(n) for n in input().split()]
	z.append(s[1:])
	k += s[1:]
l = 0
p = max(k)
for n in range(x):
	l += (p - max(z[n])) * len(z[n])
print(l)


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


z = []
m = 0
s = 0
for _ in range(int(input())):
	a = list(map(int, input().split()))
	z += [[max(a[1:]), a[0]]]
	if z[-1][0] > m:
		m = z[-1][0]
for i in z:
	s += (m - i[0]) * i[1]
print(s)


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


n = int(input())
a = []
lms = []
max = 0
for i in range(n):
	b = []
	lms.insert(i, 0)
	value = input()
	t = 0
	for c in map(int, value.split(' ')):
		b.insert(t, c)
		t += 1
	k = b[0]
	for j in range(1, k + 1):
		p = b[j]
		if p > max:
			max = p
		if p > lms[i]:
			lms[i] = p
	a.insert(i, b)
ans = 0
for i in range(n):
	ans += (max - lms[i]) * (len(a[i]) - 1)
print(ans)


n = int(input())
comps = []
g_max = 0
for i in range(n):
	m = [int(x) for x in input().split(' ')]
	comp_max = max(m[1:])
	comps.append([m[0], comp_max])
	g_max = max(comp_max, g_max)
sum_raise = 0
for (num, comp_max) in comps:
	sum_raise += (g_max - comp_max) * num
print(sum_raise)


n = int(input())
dic = []
cl = []
mxzp = 0
for i in range(n):
	s = input().split()
	m = int(s[0])
	mx = max(list(map(int, s[1:])))
	dic.append((mx, m))
	if mx > mxzp:
		mxzp = mx
res = 0
for x in dic:
	res += (mxzp - x[0]) * x[1]
print(res)


n = int(input())
z = []
for q in range(n):
	z.append(list(map(int, input().strip().split())))
temp = max(z[0][1:])
dis = z[0][0]
ans = 0
for i in range(1, n):
	x = max(z[i][1:])
	if x > temp:
		ans += (x - temp) * dis
		temp = x
		dis = dis + z[i][0]
	else:
		ans += (temp - x) * z[i][0]
		dis = dis + z[i][0]
print(ans)


n = int(input())
allemp = []
while n != 0:
	inp = list(map(int, input().split()))
	m = inp[0]
	emp = inp[1:]
	allemp.append(emp)
	n -= 1
allemp.sort(key=lambda x: max(x), reverse=True)
maxval = max(allemp[0])
inc = 0
for val in allemp[1:]:
	inc += (maxval - max(val)) * len(val)
print(inc)


n = int(input())
maxim = 0
res = []
maximn = -1
for _ in range(n):
	l = [int(i) for i in input().split()]
	curr = l[0]
	l = l[1:]
	if max(l) > maxim:
		maxim = max(l)
	res.append([max(l), curr])
ans = 0
for i in range(n):
	ans = ans + res[i][1] * (maxim - res[i][0])
print(ans)


n = int(input())
c = 0
m = 0
ma1 = []
ma2 = []
for i in range(n):
	lst = list(map(int, input().strip().split(' ')))
	m1 = max(lst[1:])
	ma1.append(m1)
	ma2.append(lst[0])
m2 = max(ma1)
for j in range(n):
	if ma1[j] != m2:
		c += (m2 - ma1[j]) * ma2[j]
print(c)


arr = []
mx = 0
n = int(input())
for i in range(n):
	s = list(map(int, input().split()))
	m = max(s[1:])
	if m > mx:
		mx = m
	arr.append([s[0], m])
need = 0
for i in range(n):
	need += (mx - arr[i][1]) * arr[i][0]
print(need)


n = int(input())
cmpn = []
for i in range(n):
	s = list(map(int, input().split()))
	s[0] = 0
	cmpn.append([max(s), len(s) - 1])
cmpn.sort()
ans = 0
mx = cmpn[n - 1][0]
for i in range(n):
	ans += (mx - cmpn[i][0]) * cmpn[i][1]
print(ans)


n = int(input())
CompS = []
S = 0
for i in range(n):
	(m, *H) = list(map(int, input().split()))
	Max = max(H)
	CompS.append((Max, m))
CompS.sort(reverse=True)
for i in range(1, len(CompS)):
	S += (CompS[0][0] - CompS[i][0]) * CompS[i][1]
print(S)


n = int(input())
maxsalary = 0
fulllist = []
for i in range(n):
	line = input()
	tokens = line.split()
	m = int(tokens[0])
	lst = []
	for j in range(m):
		lst.append(int(tokens[j + 1]))
	maxsalaryofthisline = max(lst)
	maxsalary = max(maxsalary, maxsalaryofthisline)
	fulllist.append((maxsalaryofthisline, m))
increase = 0
for lst in fulllist:
	(maxsalaryofthisline, numberofemployee) = lst
	increase += numberofemployee * (maxsalary - maxsalaryofthisline)
print(increase)


com = int(input())
comp = []
for c in range(com):
	tmp = input().split()
	num = []
	for i in tmp:
		num.append(int(i))
	comp.append(num)
maxs = []
for c in comp:
	lTmp = c[1:]
	maxs.append(max(lTmp))
maxT = max(maxs)
count = 0
for i in range(len(comp)):
	lTmp = comp[i][1:]
	if maxT == max(lTmp):
		continue
	mTmp = max(lTmp)
	dif = maxT - mTmp
	count += comp[i][0] * dif
print(count)


m = []
for _ in range(int(input())):
	b = list(map(int, input().split()))
	m.append(b)
t = []
l = []
for num in m:
	t.append(max(num[1:]))
	l.append(num[0])
u = max(t)
r = []
for j in range(len(t)):
	r.append(abs(t[j] - u) * l[j])
print(sum(r))


import sys
n = int(sys.stdin.readline())
arr = []
for _ in range(n):
	(m, *temp) = list(map(int, sys.stdin.readline().split()))
	arr.append((max(temp), m))
arr.sort(key=lambda x: -x[0])
val = arr[0][0]
ans = 0
for i in range(n):
	ans += abs(arr[i][0] - val) * arr[i][1]
print(ans)


n = int(input())
l1 = []
m1 = -1
l2 = []
l4 = []
for i in range(n):
	l1 = list(map(int, input().split()))
	l3 = l1[1:]
	m = max(l3)
	if m > m1:
		m1 = m
	l2.append(m)
	l4.append(l1)
c = 0
for j in range(n):
	c = c + (m1 - l2[j]) * l4[j][0]
print(c)


n = int(input())
ans = 0
sm = 0
mxall = 0
for _ in range(n):
	seq = list(map(int, input().split()))
	x = seq[0]
	del seq[0]
	mx = max(seq)
	if mx < mxall:
		ans += x * (mxall - mx)
	else:
		ans += sm * (mx - mxall)
		mxall = mx
	sm += x
print(ans)


n = int(input())
mx = 0
res = 0
v = []
for i in range(n):
	k = input().split(' ')
	tmx = 0
	for j in range(1, len(k)):
		if tmx < int(k[j]):
			tmx = int(k[j])
	v.append([int(k[0]), tmx])
	if tmx > mx:
		mx = tmx
for i in v:
	res += i[0] * (mx - i[1])
print(res)


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	m = []
	c = []
	finn = -1
	for i in range(n):
		m.append(list(map(int, input().split()))[1:])
	for i in range(n):
		finn = max(finn, max(m[i]))
		c.append([max(m[i]), len(m[i])])
	c.sort()
	c.reverse()
	ans = 0
	currlen = 0
	for i in range(n - 1, 0, -1):
		ans += c[i][1] * (-c[i][0] + finn)
	print(ans)
main()


import functools
cost = 0

def reduce_func(x, y):
	global cost
	cost += x[0] * (y[1] - x[1])
	return [x[0] + y[0], y[1]]
n = int(input())
p = []
for i in range(n):
	c = list(map(int, input().split()))
	m = c[0]
	c = c[1:]
	a = max(c)
	p.append([m, a])
p.sort(key=lambda x: x[1])
output = functools.reduce(reduce_func, p)
print(cost)


t = int(input())
(max1, n) = ([], [])
for _ in range(t):
	li = list(map(int, input().split()))
	max1.append(max(li[1:]))
	n.append(li[0])
maximum = max(max1)
ans = 0
for i in range(len(max1)):
	if max1[i] == maximum:
		continue
	ans += (maximum - max1[i]) * n[i]
print(ans)