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

Difficulty: interview

Starter code:

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

# A. Company Merging
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)


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)


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

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(list(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 - общая наибольшая зарплата
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)


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)


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)


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]:#1 больше 2
        u = zar[0][1] - zar[i+1][1]
        y+=u*zar[i+1][0]
    else:#2 больше 1
        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())
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)
               
               


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)
               
               


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

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

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)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec  9 14:00:51 2018

@author: mach
"""

i = int(input())
l = []
for _ in range(i):
    k = list(map(int, input().strip().split()))
    l.append(k)

#//l = [[2,4,3],[2,2,1],[3,1,1,1]]
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)


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)


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)


# -*- coding: utf-8 -*-
# @Date    : 2018-12-10 10:06:33
# @Author  : raj lath (oorja.halt@gmail.com)
# @Link    : link
# @Version : 1.0.0

from sys import stdin

max_val=int(10e12)
min_val=int(-10e12)

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)




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