Company: Arcesium_19dec
Difficulty: medium
Hydrate the nodes There is a tree with n nodes, rooted at node 0 . As usual in computer science the tree grows upside down compared to trees in nature: node 0 is at the top and every other node hangs below its parent. An apple grows on every node. Apple i has a water level waterLevel[i] , which is one of: -1 — almost underhydrated , 0 — neither almost underhydrated nor almost overhydrated, 1 — almost overhydrated . You pour water on exactly one node v . Pouring on v hydrates every apple in the subtree of v — that is v itself and all of its descendants. Afterwards: every hydrated apple that was almost overhydrated ( waterLevel = 1 ) becomes overhydrated , and costs overhydratedPenalty cents; every apple of the whole tree that was almost underhydrated ( waterLevel = -1 ) and that received no water becomes underhydrated , and costs underhydratedPenalty cents. An apple with waterLevel = 0 never costs anything, and an almost underhydrated apple that lies inside the watered subtree costs not