#D519. Tree of Peony
Tree of Peony
Tree of Peony
Problem statement
There is a tree consisting of vertices edges, and the th edge connects the and vertices.
Each vertex has a button, and the button at the th vertex is called the button .
First, the beauty of the button is .
Each time you press the button , the beauty of the button is given to the adjacent buttons by .
That is, if the th vertex is adjacent to the th vertex, the beauty of the button is reduced by , and the button increases by each.
At this time, you may press the button with negative beauty, or the beauty of the button as a result of pressing the button may become negative.
Your goal is to make the beauty of buttons , respectively.
How many times in total can you press the button to achieve your goal?
Constraint
- All inputs are integers
- The graph given is a tree
- The purpose can always be achieved with the given input
input
Input is given from standard input in the following format.
output
Output the minimum total number of button presses to achieve your goal.
Input example 1
Four 1 2 13 3 4 0 3 2 0 -3 4 5 -1
Output example 1
Four
You can achieve your goal by pressing the button a total of times as follows, which is the minimum.
- Press the button times. The beauty of the buttons changes to , respectively.
- Press the button times. Beauty changes to respectively.
- Press the button times. Beauty changes to respectively.
Input example 2
Five 1 2 13 3 4 3 5 -9 5 7 1 8 -7 4 5 1 9
Output example 2
3
Example
Input
4 1 2 1 3 3 4 0 3 2 0 -3 4 5 -1
Output
4
inputFormat
inputs are integers
- The graph given is a tree
- The purpose can always be achieved with the given input
input
Input is given from standard input in the following format.
outputFormat
output
Output the minimum total number of button presses to achieve your goal.
Input example 1
Four 1 2 13 3 4 0 3 2 0 -3 4 5 -1
Output example 1
Four
You can achieve your goal by pressing the button a total of times as follows, which is the minimum.
- Press the button times. The beauty of the buttons changes to , respectively.
- Press the button times. Beauty changes to respectively.
- Press the button times. Beauty changes to respectively.
Input example 2
Five 1 2 13 3 4 3 5 -9 5 7 1 8 -7 4 5 1 9
Output example 2
3
Example
Input
4 1 2 1 3 3 4 0 3 2 0 -3 4 5 -1
Output
4
样例
4
1 2
1 3
3 4
0 3 2 0
-3 4 5 -1
4