{"problem":{"name":"G. Robots","description":{"content":"The Robotics Olympiad teams were competing in a contest.  There was a tree drawn on the floor, consisting of n nodes and n - 1 edges. The nodes are numbered from 1 to n, and each edge has a weight. T","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":5000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF10191G"},"statements":[{"statement_type":"Markdown","content":"The Robotics Olympiad teams were competing in a contest. \n\nThere was a tree drawn on the floor, consisting of n nodes and n - 1 edges. The nodes are numbered from 1 to n, and each edge has a weight. The tree is rooted at the first node. q teams are participating, and each team is given an integer xi. Their robot should start at node 1, and move in the following way until there are no valid moves left: From all the edges between the current node and it's children, go through the edge with the maximum value less than xi. Note that the robot can't move to the parent, only to children. \n\n However, the teams weren't able to program the robots to return to them after the contest, so they had to manually pick them up. Since the tree can be quite large, they need your help to determine where each robot ended it's movement.\n\nThe first line contains a single integer T, the number of test cases. \n\nThe first line of each test case contains two space-separated integers n and q. (1 ≤ n, q ≤ 105). \n\nThe following n - 1 lines contain 3 integers ui, vi, wi. This means that there is an edge connecting nodes ui and vi, with weight wi. (1 ≤ ui, vi ≤ n) (1 ≤ wi ≤ 109). It's guaranteed that all wi are distinct.\n\nThe following line contains q integers xi. (1 ≤ xi ≤ 109).\n\nFor each test case, print one line with a single number Si, the sum of numbers of nodes where each robot ends.\n\nIn the sample test case, the robots end in the following nodes: {1, 1, 2, 5, 5, 3, 4}. \n\nSi = 1+1+2+5+5+3+4 = 21.\n\nLarge I/O files. Please consider using fast input/output methods.\n\n## Input\n\nThe first line contains a single integer T, the number of test cases. The first line of each test case contains two space-separated integers n and q. (1 ≤ n, q ≤ 105). The following n - 1 lines contain 3 integers ui, vi, wi. This means that there is an edge connecting nodes ui and vi, with weight wi. (1 ≤ ui, vi ≤ n) (1 ≤ wi ≤ 109). It's guaranteed that all wi are distinct.The following line contains q integers xi. (1 ≤ xi ≤ 109).\n\n## Output\n\nFor each test case, print one line with a single number Si, the sum of numbers of nodes where each robot ends.\n\n[samples]\n\n## Note\n\nIn the sample test case, the robots end in the following nodes: {1, 1, 2, 5, 5, 3, 4}. Si = 1+1+2+5+5+3+4 = 21.Large I/O files. Please consider using fast input/output methods.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ T \\in \\mathbb{Z} $ be the number of test cases.  \nFor each test case $ k \\in \\{1, \\dots, T\\} $, let three fractions be given as $ \\frac{n_{k,1}}{d_{k,1}}, \\frac{n_{k,2}}{d_{k,2}}, \\frac{n_{k,3}}{d_{k,3}} $, where $ n_{k,i}, d_{k,i} \\in \\mathbb{Z}^+ $ and $ 1 \\leq n_{k,i}, d_{k,i} \\leq 10^6 $.\n\n**Constraints**  \n1. $ 1 \\leq T \\leq 10^3 $  \n2. For each $ k \\in \\{1, \\dots, T\\} $ and $ i \\in \\{1,2,3\\} $:  \n   $ 1 \\leq n_{k,i} \\leq 10^6 $, $ 1 \\leq d_{k,i} \\leq 10^6 $\n\n**Objective**  \nFor each test case $ k $, compute the sum:  \n$$\nS_k = \\frac{n_{k,1}}{d_{k,1}} + \\frac{n_{k,2}}{d_{k,2}} + \\frac{n_{k,3}}{d_{k,3}}\n$$  \nand output the result as a simplified fraction $ \\frac{\\text{num}}{\\text{den}} $, where $ \\gcd(\\text{num}, \\text{den}) = 1 $.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF10191G","tags":[],"sample_group":[],"created_at":"2026-03-03 11:00:39"}}