atcoder#AGC023F. [AGC023F] 01 on Tree
[AGC023F] 01 on Tree
Score : points
Problem Statement
Snuke has a rooted tree with vertices, numbered through . Vertex is the root of the tree, and the parent of Vertex ( ) is Vertex ( ). There is a number, or , written on each vertex. The number written on Vertex is .
Snuke would like to arrange the vertices of this tree in a horizontal row. Here, for every vertex, there should be no ancestor of that vertex to the right of that vertex.
After arranging the vertices, let be the sequence obtained by reading the numbers written on the vertices from left to right in the arrangement. Snuke would like to minimize the inversion number of . Find the minimum possible inversion number of .
Notes
The inversion number of a sequence whose length is the number of pairs of integers and ( ) such that .
Constraints
- ( )
- ( )
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum possible inversion number of .
6
1 1 2 3 3
0 1 1 0 0 0
4
When the vertices are arranged in the order , will be , whose inversion number is . It is impossible to have fewer inversions, so the answer is .
1
0
0
, whose inversion number is .
15
1 2 3 2 5 6 2 2 9 10 1 12 13 12
1 1 1 0 1 1 0 0 1 0 0 1 1 0 0
31