In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.
Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some interesting features:
Byteasar is from the old town of Byteburg and, like all true Byteburgers, praises neatness and order. He wonders how neat can his tree become thanks to appropriate rotations. The neatness of a tree is measured by the number of inversions in its corona, i.e. the number of pairs , such that in the corona .
The original tree (on the left) with corona has two inversions.
A single rotation gives a tree (on the right) with corona , which has only one inversion.
Each of these two trees has 5 branches.
Write a program that determines the minimum number of inversions in the corona of Byteasar's tree that can be obtained by rotations.
In the first line of the standard input there is a single integer () that denotes the number of leaves in Byteasar's tree. Next, the description of the tree follows. The tree is defined recursively:
In the first and only line of the standard output a single integer is to be printed: the minimum number of inversions in the corona of the input tree that can be obtained by a sequence of rotations.
For the input data:
3 0 0 3 1 2
the correct result is:
1
Explanation of the example: Figure 1 illustrates the tree given in the example.
Task author: Tomasz Walen.