Initializing model...

Apple Tree

hard

A tree has N nodes, each containing some apples. Starting from node 1, you can take at most K steps along edges. At each node you visit, you eat all the apples there.

Return the maximum number of apples you can eat.

**Input:** `n` (number of nodes), `k` (max steps), `apples` (list of apple counts per node, 1-indexed), `edges` (list of [u, v] pairs).

**Constraints:** 1 ≤ N ≤ 100, 0 ≤ K ≤ 200

*Classic tree DP problem (POJ 2486, authored by magicpig@ZSU)*

Function Signature

def apple_tree(n: int, k: int, apples: list[int], edges: list[list[int]]) -> int:

Examples

Input: n=2, k=1, apples=[0,0,11], edges=[[1,2]]
Output: 11
Walk from node 1 (0 apples) to node 2 (11 apples) in 1 step.
Input: n=3, k=2, apples=[0,0,1,2], edges=[[1,2],[1,3]]
Output: 2
Go to node 3 (2 apples) and back. Can't visit both children in 2 steps.