We call a vertex dead if it has $$$0$$$ nuts, and if we root the tree in it, among the subtrees of its children, there is at most one subtree with a positive sum of the numbers of nuts.
We denote the movement operation for a vertex $$$v$$$ as $$$\operatorname{c}(v)$$$.
Let $$$A$$$ be the forest consisting of all living vertices. We prove that $$$A$$$ is always connected, i.e., it is a tree. Suppose for a contradiction, $$$A$$$ has at least two connected components, then the path between them must contain a dead vertex $$$v$$$. However, $$$v$$$ is alive, since both connected components are in different subtrees of $$$v$$$'s children when the tree's rooted in $$$v$$$, a contradiction, q.e.d.
Let $$$\deg(v)$$$ be the degree of $$$v$$$ in $$$A$$$. It's easy to see that when applying $$$\operatorname{c}(v)$$$, $$$a_v := a_v + \deg(v)$$$, and for all other vertices $$$u \in A$$$, $$$a_u := a_u + \deg(u) - 2$$$. If $$$v \not \in A$$$, then $$$a_v := a_v + 1$$$, $$$a_u := a_u + \deg(u) - 2$$$, $$$a_p := a_p + \deg(p) - 1$$$, where $$$p$$$ is the closest vertex from $$$A$$$ to $$$v$$$. $$$[*]$$$
For a sequence of operations, we define the tree $$$L$$$ as $$$A$$$ at the moment after all $$$\operatorname{c}(v)$$$ operations and before the nuts are eaten.
Suppose we have a sequence of operations (including the eating) that results in no nuts remaining. Let us prove that the last operation $$$\operatorname{c}(v)$$$, where $$$v \not \in L$$$, can be replaced by $$$\operatorname{c}(u)$$$, where $$$u \in L$$$. In this case, $$$A$$$ becomes a non-strict subset of itself, and again, there are no nuts left. Consider the moment before applying $$$\operatorname{c}(v)$$$. There are two options:
$$$v$$$ is alive. Then, when replacing $$$\operatorname{c}(v)$$$ with $$$\operatorname{c}(u)$$$, $$$a_v$$$ decreases by $$$2$$$, and $$$a_u$$$ increases by $$$2$$$ according to $$$[*]$$$.
$$$v$$$ is dead. Let $$$p$$$ be the closest vertex in $$$A$$$ to $$$v$$$. Then, when replacing $$$\operatorname{c}(v)$$$ with $$$\operatorname{c}(u)$$$, $$$a_v$$$ and $$$a_u$$$ will decrease by $$$1$$$ (become equal to $$$0$$$), and $$$a_p$$$ will increase by $$$1$$$ according to $$$[*]$$$.
In both cases, all $$$a_x$$$ for $$$x \not \in L$$$ will decrease, and it is easy to see that the sequence of operations after $$$\operatorname{c}(v)$$$ will also bring all nuts into $$$L$$$, after which the same eating operations will remove all nuts, q.e.d. Thus, all operations of type $$$\operatorname{c}(v)$$$, where $$$v \not \in L$$$, can be (sequentially from the end) replaced by $$$\operatorname{c}(u)$$$ for some $$$u \in L$$$.
Now we have to solve the following problem:
We need to choose a subtree $$$L$$$ of the original tree, then repeatedly apply $$$\operatorname{c}(u)$$$, $$$u \in L$$$, to make all vertices not in $$$L$$$ dead, and then apply the eat operation to all $$$v \in L$$$. Note that by $$$[*]$$$, all vertices not in $$$L$$$ will become dead regardless of which vertices in $$$L$$$ we choose; in other words, we can apply all $$$\operatorname{c}(v)$$$ to only one vertex $$$v$$$.
Consider the dynamics along directed edges $$$xy$$$, $$$dp[x][y]$$$: the smallest number of $$$\operatorname{c}(v)$$$ operations, where $$$v$$$ is on the $$$x$$$ side of $$$xy$$$, needed to make all vertices on the $$$y$$$ side (including $$$y$$$) of $$$xy$$$ dead. The value of $$$a_y$$$ can only decrease when $$$\deg(y) = 1$$$, so $$$dp[x][y] = \sum_{i=0}^{k}dp[y][ch[i]]$$$, that is, $$$dp[x][y]$$$ is equal to the sum of $$$a_v$$$ over all $$$v$$$ in the subtree $$$y$$$, if we hang the entire tree on $$$x$$$.
For all vertices $$$v \not \in L$$$, at some point and always after that, $$$a_v = 0$$$. It is also possible that $$$a_v = 0$$$ before all operations; $$$a_v$$$ is never equal to $$$0$$$ again.
We define $$$zero[y]$$$ to be the smallest number of $$$\operatorname{c}(v)$$$ operations required for $$$a_y = 0$$$ after any number of $$$\operatorname{c}(v)$$$ operations at least equal to $$$zero[y]$$$. $$$zero[y] = \min_x({dp[x][y]})$$$ for all $$$x$$$ adjacent to $$$y$$$, and $$$zero[y] = 0$$$ if $$$a_y = 0$$$ and $$$\deg(y) = 2$$$ initially. This is true because if $$$a_y \ne 0$$$ or $$$\deg(y) \ne 2$$$, $$$y$$$ will finally "zero out" only when it becomes dead. $$$\min_x({dp[x][v]})$$$ is greater than $$$dp[v][u]$$$ for all, possibly except one, vertices $$$u$$$ adjacent to $$$v$$$, and therefore is greater than $$$\min_t({dp[t][u]})$$$ for $$$t$$$ adjacent to $$$u$$$ adjacent to $$$v$$$. Therefore, applying $$$\operatorname{c}(v)$$$ to $$$v$$$ with the largest $$$zero[v]$$$, each $$$a_u$$$ will become equal to $$$0$$$ after $$$zero[u]$$$ operations.
Thus, either there are no $$$\operatorname{c}(x)$$$ operations, and the eating operation must be applied to all $$$y$$$ with initial $$$a_y = 0$$$, or there are $$$\operatorname{c}(x)$$$ $$$k$$$ operations, and the eating operation must be applied to all vertices $$$y$$$ with $$$zero[y] \ge k$$$. For the first case, we simply calculate the cost of this sequence of operations. For the second case, we sort $$$zero$$$ in $$$O(n \log n)$$$, then iterate over the number of "zeroed" vertices, iterating over $$$zero$$$, and calculate the answer as $$$(n - i) \cdot q + zero[i - 1] \cdot p$$$. The answer to the problem will be the minimum of the obtained ones.
Total complexity: $$$O(n \log n)$$$.