##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
1 3 3 2 3 1 1 2 3 2 3 4
12
1 1 0 5
0
5 3 3 2 3 1 1 2 3 2 3 4 1 0 5 4 100 1 2 3 4 1 2 5 2 3 5 3 4 5 2 5 10 20 1 2 10 4 0 1 1 1 1 1 2 1 2 3 1 3 4 1
12 0 0 30 12
Rayan discovered ancient temples connected by mystical pathways. The temples and pathways form a tree, meaning there is exactly one path between any two temples.
Each temple has a power level , and each pathway has a length .
For two temples and , let denote the sum of pathway lengths on the unique path between them.
Two temples are distant friends if the path length between them is at least .
For each temple , compute its contribution:
Find the sum of contributions over all temples.
Input: n=3, D=3, powers=[2,3,1]
Tree edges: 1-2 (weight 3), 2-3 (weight 4)
Find sum of power products for pairs with distance ≥ D.
Answer: 12
Input: n=1, D=0, powers=[5]
Single node, no pairs.
Answer: 0
Input: 5 test cases with various tree configurations.
Answer: 12 0 0 30 12