Company Merging

题意翻译

## 题目描述 一个由n(1≤n≤2·10^5)个公司组成的企业,每个公司中有mi(1≤m≤2·10^5)个员工,每个公司员工都有工资(≤10^9)。现在,公司老板想要将这n个公司合并,合并的规则为:保证每个公司的最高工资相等,并且工资只能增不能减,一个公司中的员工的工资必须同时增加相同的钱数。 现在老板想要知道最少增加多少钱的工资可以将这些公司合并。 ## 输入输出格式 ### 输入格式: 第一行为n。 接下来n行每行为mi以及mi个数,代表第i家公司的人数以及所有员工的工资。 ### 输出格式: 一个整数,表示将所有公司合并需要的最小钱数。 ## 说明 第一家公司工资不变,第二家公司所有人工资加2,变为{4,3},第三家工资都加3,变为{4,4,4},共需2+2+3+3+3=13块钱。

题目描述

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.

输入输出格式

输入格式


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 a single integer — the minimal total increase of all employees that allows to merge all companies.

输入输出样例

输入样例 #1

3
2 4 3
2 2 1
3 1 1 1

输出样例 #1

13

说明

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