Company: SIXT
Difficulty: medium
Max Nodes on Upward Path Problem Description You are given a rooted tree with N nodes labelled 1 to N . The root is node 1 . Node i holds A[i] candies. You pick any starting node u . You collect the candies of u , then move to the parent of u and collect its candies, then to that node's parent, and so on towards the root. Every node you visit is collected in full. You must stop before a node whose candies would make your total exceed K , and you also stop after collecting the root. You may not skip a node to reach a cheaper ancestor. In other words, the nodes you collect always form an upward path : a starting node followed by a run of its consecutive ancestors. Find the maximum number of nodes on an upward path whose total number of candies is at most K . Complete the function solve with the following parameters: N : the number of nodes K : the largest total number of candies you may collect A : an array of N integers, where A[i - 1] is the candy count of node i edges : an array of N