| OCPC 2024 Summer, Day 4: wuhudsm Contest |
|---|
| Закончено |
You are given a tree with $$$n$$$ nodes and a fixed even integer $$$k$$$. Node $$$i$$$ has weight $$$a_i$$$.
A closed path on a tree is a path that starts and ends at the same node. A closed path may visit the same node multiple times. A closed path of length $$$k$$$ that starts at node $$$1$$$ can be written as $$$x_1 \rightarrow x_2 \rightarrow \ldots \rightarrow x_{k+1}$$$, where:
The score of the closed path $$$x_1 \rightarrow x_2 \rightarrow \ldots \rightarrow x_{k+1}$$$ is defined as $$$\sum_{i=1}^{k+1} a_{x_i}$$$.
You need to answer $$$q$$$ queries. In the $$$i$$$-th query, you are given two numbers $$$p_i$$$ and $$$v_i$$$, which means you should update the value of $$$a_{p_i}$$$ to $$$v_i$$$. After the update, you need to find the score of a closed path of length $$$k$$$ starting at node $$$1$$$ that has the maximum possible score. Note that the updates are permanent.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 2.5 \cdot 10^4$$$), the number of test cases.
For each test case:
It is guaranteed that the sum of $$$n$$$ and $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ respectively.
For each query, output a single integer in a new line — the maximum score of a closed path of length $$$k$$$ starting at node $$$1$$$ after applying the update.
24 61 4 3 11 21 34 322 34 22 10000000001 11 241 10000000002 10000000001 12 1
13 15 500000001500000000 1000000001000000000 500000000500000001 1000000001
In the first query of the first test case, $$$a=[1,3,3,1]$$$ and $$$k=6$$$. One of the possible closed paths of length $$$k$$$ that starts at node $$$1$$$ is $$$1 \rightarrow 2 \rightarrow 1 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 1$$$. The score of this closed path is $$$13$$$.
It can be proven that $$$13$$$ is the maximum possible score of a closed path.
In the first query of the first test case, $$$a=[1,3,3,2]$$$ and $$$k=6$$$. One of the possible closed paths of length $$$k$$$ that starts at node $$$1$$$ is $$$1 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 1$$$. The score of this closed path is $$$15$$$.
It can be proven that $$$15$$$ is the maximum possible score of a closed path.
| Название |
|---|


