Lab planning | 프로그래밍의 벗 PivotOJ
PivotOJ

Lab planning

시간 제한: 1000ms메모리 제한: 1024MB출처: Programmeringsolympiaden 2016 — finalBOJ 21326

문제

Ah, university. A bastion of knowledge, the Mecca of education, and the core of bureaucracy.

Wille and Kaski are currently waiting in a computer lab to present a lab in a course. In total, there are NN groups waiting to present their lab. Group ii is going to present mim_i different labs, where lab jj takes ai,ja_{i, j} minutes.

To be fair, all groups need to wait a really long time to present. There is only a single teacher taking presentations, and letting each group present all their labs in a single go would mean that they don't have to wait unnecessarily for other groups! Therefore, the teacher takes presentations in a seemingly arbitrary order...

Kashi has had enough. She wants to go home and play Pokémon, and is complaining about the inefficiencies in the presentation scheme. To demonstrate how inefficient the system is, she'll compute the longest possible total waiting time, and show just how close the teacher's system is to this value.

Assume that a group starts presenting their first lab at time aa, and completes their final lab at time bb. Then its waiting time is bab - a. The total waiting time is the sum of the waiting time over all groups.

The order in which a group must present its labs must be the same as the order they are given in the input.

입력

The first line contains a single integer N1N \ge 1.

Then follow NN lines, each containing first the number mi1m_i \ge 1, and then mim_i integers ai,ja_{i,j}, all between 1 och 60.

출력

You should output a single number: the longest possible total waiting time for the students over all possible presentation orders.

힌트

In the given sample, three groups need to present their labs: the first group has labs taking 5 and 15 minutes, the second group takes 10 and 20 minutes, and the third group has a single lab taking an entire hour.

If we choose the order 5,10,60,20,155, 10, 60, 20, 15, the third group will wait only for 60 minutes. The second group will, besides their own labs, have to wait for the lab of the third group, and therefore take 10+60+20=9010+60+20 = 90 minutes. The first group will instead take 5+10+60+20+15=1105 + 10 + 60 + 20 + 15 = 110 minutes.

The sum of this is 60+90+110=26060 + 90 + 110 = 260 minutes.

Note that the order 10, 15, 60, 20, 5 would have resulted in a worse time of 265265, but this is not allowed: group 1 may not present their second lab before their first.

예제

예제 1

입력
3
2 5 15
2 10 20
1 60
출력
260
코드를 제출하려면 로그인하세요.