Nutrient Tree
Time limit2sMemory limit512 MB
Given a binary tree whose leaves produce nutrients and whose edges have capacity (1+w)^2 after spending w agents, distribute X agents over edges and leaves to maximize the flow reaching the root.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, DFS, Greedy
- Solved
- No attempts yet
Problem
You are given a binary tree with leaves (). Each leaf produces an amount of nutrients ().
Every branch of the tree (think of it as an edge) limits how many nutrients can flow along it toward the root. You have growth agents (), and each agent can be spent in one of two ways:
- Thicken an edge. Every edge starts with weight . If you assign growth agents to an edge, that edge can carry at most nutrients.
- Boost a leaf. If you assign growth agents to a leaf whose initial value is , its production becomes .
Nutrients flow from the leaves up toward the root. The amount that travels along an edge is the smaller of the edge's capacity and the amount arriving from below. Where edges meet at an internal node, the incoming amounts are summed before continuing upward.
Determine the maximum amount of nutrients that can reach the root.
Input
The first line contains a description of the tree, defined recursively:
- an integer () denotes a single leaf that produces nutrients, or
- denotes an internal node whose left and right subtrees are described by and (the two subtree descriptions are separated by a space and wrapped in parentheses).
The second line contains the integer (), the number of growth agents you have.
Output
Print a single line containing the maximum amount of nutrients that can reach the root of the tree.