eolymp
bolt
Try our new interface for solving problems
Problems

Tree Painting

Tree Painting

You are given a tree (an undirected connected acyclic graph) consisting of $n$ vertices. You are playing a game on this tree. Initially, all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn you choose a white vertex adjacent (connected by an edge) to any black vertex and paint it black. Each time you choose a vertex (even during the first turn), you gain a number of points equal to the size of the connected component consisting only of white vertices that contains the chosen vertex. The game ends when all vertices are painted black. Let's see the following example: \includegraphics{https://static.eolymp.com/content/25/257f45b5c95bd6596433884ebda98b05762161c6.gif} Vertices $1$ and $4$ are painted black already. If you choose vertex $2$, you will gain $4$ points for the connected component consisting of vertices $2, 3, 5$ and $6$. If you choose vertex $9$, you will gain $3$ points for the connected component consisting of vertices $7, 8$ and $9$. Your task is to maximize the number of points you gain. \InputFile The first line contains an integer $n~(2 \le n \le 2 \cdot 10^5)$ --- the number of vertices in the tree. Each of the next $n - 1$ lines describes an edge of the tree. Edge $i$ is denoted by two integers $u_i$ and $v_i~(1 \le u_i, v_i \le n, u_i \ne v_i)$, the indices of vertices it connects. It is guaranteed that the given edges form a tree. \OutputFile Print one integer --- the maximum number of points you gain if you play optimally.
Time limit 1 second
Memory limit 128 MiB
Input example #1
9
1 2
2 3
2 5
2 6
1 4
4 9
9 7
9 8
Output example #1
36
Input example #2
5
1 2
1 3
2 4
2 5
Output example #2
14