luogu#P9294. [POI 2020/2021 R1] Cukiernia / 糕点店

[POI 2020/2021 R1] Cukiernia / 糕点店

Background

This problem is translated from XXVIII Olimpiada Informatyczna – Stage I Cukiernia

Problem Description

The Bajtuś bakery sells three kinds of food: cakes, donuts, and croissants. In the bakery, there are nn display shelves. Under normal conditions, each shelf should contain only one kind of food. But one morning, the bakery owner Bajtazara’s son Bajtuś sneaked into the bakery and messed up all the food placement.

The bakery is about to open. Bajtazara urgently wants to rearrange the food so that each shelf contains only one kind of food (in particular, it is also allowed for a shelf to be empty). Please help him find the minimum number of moves needed to achieve this goal.

Input Format

The first line contains an integer nn, representing the number of shelves in the bakery.

The next nn lines each contain three integers di,pi,rid_i, p_i, r_i, representing the current number of cakes, donuts, and croissants on the ii-th shelf. The testdata guarantees that there is at least one piece of food in the bakery.

Output Format

Output one integer, the minimum number of moves required to move the food.

5
5 1 1
0 3 4
1 4 3
4 0 0
0 0 0
9
3
1 1 2
2 1 1
1 1 2
7
5
5 5 5
5 5 5
5 5 5
5 5 5
5 5 5
50

Hint

[Sample Explanation #1]:

One valid moving plan is as follows:

  1. Move one donut from shelf 11 to shelf 33, and move one croissant from shelf 11 to shelf 22.
  2. Move three donuts from shelf 22 to shelf 33.
  3. Move one cake from shelf 33 to shelf 11, and move three croissants from shelf 33 to shelf 22.

After that, shelf 11 contains only cakes, shelf 22 contains only croissants, shelf 33 contains only donuts, shelf 44 contains only cakes, and shelf 55 is empty.

[Constraints]:

All test points satisfy: 3n3×1053 \leq n \leq 3 \times 10^5, 0di,pi,ri1090 \leq d_i, p_i, r_i \leq 10^9.

Subtask ID nn \leq Score
11 1010 1515
22 5×1035 \times 10^3 3535
33 3×1053 \times 10^5 5050

Translated by ChatGPT 5