codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1090A.md (2239B)


      1 Source: https://codeforces.com/problemset/problem/1090/A
      2 
      3 A. Company Merging
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 512 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 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.
     24 
     25 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.
     26 
     27 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.
     28 
     29 Input
     30 
     31 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.
     32 
     33 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$.
     34 
     35 The total number of employees in all companies does not exceed $2 \cdot 10^5$.
     36 
     37 Output
     38 
     39 Output a single integer — the minimal total increase of all employees that allows to merge all companies.
     40 
     41 Example
     42 
     43 Input
     44 
     45 ```
     46 3
     47 2 4 3
     48 2 2 1
     49 3 1 1 1
     50 ```
     51 
     52 Output
     53 
     54 ```
     55 13
     56 ```
     57 
     58 Note
     59 
     60 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$.