##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
3
3 -100 5 -100 1 2 2 3
4 10 7 8 9 1 2 1 3 1 4
5 -1 -1 3 -1 -1 1 2 2 3 3 4 4 5
5
34
3
You are given a tree with vertices. Each vertex has an integer value (it may be negative).
In one operation, choose three distinct vertices such that is adjacent to and is adjacent to , so is a path of length and is the middle vertex.
Then vertices and are deleted, and vertex stays. For every former neighbor of with , add an edge between and . For every former neighbor of with , add an edge between and . The value of remains . After this operation, the graph is still a tree.
You may perform any number of operations (possibly zero). Your goal is to maximize the sum of values of the remaining vertices.
Apply an operation on path .
Don't apply any operations.
Apply an operation on path to obtain path , and then apply an operation on this path.