2920. Maximum Points After Collecting Coins From All Nodes
There exists an undirected tree rooted at node
0withnnodes labeled from0ton - 1. You are given a 2D integer arrayedgesof lengthn - 1, whereedges[i] = [ai, bi]indicates that there is an edge between nodesaiandbiin the tree. You are also given a 0-indexed arraycoinsof sizenwherecoins[i]indicates the number of coins in the vertexi, and an integerk.Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected.
Coins at
nodeican be collected in one of the following ways:
- Collect all the coins, but you will get
coins[i] - kpoints. Ifcoins[i] - kis negative then you will loseabs(coins[i] - k)points.- Collect all the coins, but you will get
floor(coins[i] / 2)points. If this way is used, then for all thenodejpresent in the subtree ofnodei,coins[j]will get reduced tofloor(coins[j] / 2).Return the maximum points you can get after collecting the coins from all the tree nodes.