P12532 [XJTUPC 2025] Primal Core Optimization: Attribute Balance
Description
You are playing a game called "Rhythm Sin". In this game, you control partners to explore the "World".
Each partner has three attributes: $\mathrm{Stop}$ (S), $\mathrm{Flag}$ (F), and $\mathrm{Ever}$ (E). When all partners' attributes are completely consistent, they can successfully fuse.
Your goal is to fuse all partners, meaning you must make every pair of partners have identical attributes. To achieve this, you need to modify their attributes. Each modification consists of the following steps:
- Select any partner as the target (you can only select one partner).
- Choose any subset of her attributes.
- Increment or decrement all selected attributes by $1$ (the same operation must be carried out simultaneously, i.e., all selected attributes increment or all selected attributes decrement).
Due to limited magical power, each modification costs $1$ primal stone. Your task is to determine the minimum number of primal stones required to achieve your goal.
Input Format
The first line contains an integer $N$ ($1\le N\le 10^5$), the number of partners.
The next $N$ lines each contain three integers $S_i$, $F_i$, and $E_i$ ($1\le S_i, F_i, E_i \le 10^5$), separated by a space, representing the values of the three attributes for the $i$-th partner.
Output Format
A single integer representing the minimum number of primal stones required.
Explanation/Hint
For the first sample, one possible sequence of modifications (costing $2$ primal stones) is:
- Select partner $3$ and S attribute, modify her attributes to $2$, $2$, $3$;
- Select partner $3$ and E attribute, modify her attributes to $2$, $2$, $2$.
It can be proved that you cannot achieve the goal using less than $2$ primal stones, so the answer is $2$.
For the second sample, one possible sequence of modifications (costing $4$ primal stones) is:
- Select partner $1$ and S and E attributes, modify her attributes to $3$, $3$, $2$;
- Select partner $2$ and F and E attributes, modify her attributes to $3$, $4$, $2$;
- Select partner $2$ again and F attribute, modify her attributes to $3$, $3$, $2$;
- Select partner $3$ and F and E attributes, modify her attributes to $3$, $3$, $2$.
It can be proved that you cannot achieve the goal using less than $4$ primal stones, so the answer is $4$.
For the third sample, because there is only one partner, you don't need to perform any modifications, so the answer is $0$.