## CCC '10 S5 - Nutrient Tree

View as PDF

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
##### Canadian Computing Competition: 2010 Stage 1, Senior #5

There is a binary tree with leaves ( ) where each leaf has an amount of nutrients ( ) that it produces.

The branches (which you can think of as edges) of this binary tree limit the maximum amount of nutrients that can flow to the root of the tree. You have growth agents ( ) that can be used to increase the thickness of an edge or increase the nutrient production of a leaf node. Initially, each edge has a weight of and if you give it growth agents then it can transport nutrients. Increasing the nutrient production of a leaf with initial value by raises the nutrient production of that leaf to .

Notice that when edges meet, the amount of nutrient that flows is the sum of nutrients flowing along the incoming edges.

Find the maximum amount of nutrients you can transport to the root.

#### Input Specification

The first line of input will be a description of the tree. This description can be defined recursively as either an integer ( ) or as where and are descriptions of the left and right subtrees, respectively. The second line of input will be the integer , the amount of growth agents you have. Note: at least 30% of the marks for this question have and .

#### Output Specification

On one line, output the maximum amount of nutrients that can flow into the root of the tree.

#### Sample Input

(5 ((7 1) (3 4)))
3

#### Output for Sample Input

7

#### Explanation of Output for Sample Input

The tree description can be illustrated as: Notice that if we put two growth factors on the left edge of the root, and one growth factor on the right edge of the root. Then, there will be nutrients flowing from the leaf labelled to the root (since the capacity is units of nutrients) and nutrients flowing from the right subtree (since the capacity is , but there are only units of nutrients due to limits of on the edges attached to all other leaves).

Alternatively, notice that increasing the nutrient production of the left leaf of the root to , and increasing the capacity of the root to the left leaf by (to give a capacity of units) would cause units of nutrients to flow into the root from the left tree, and the right subtree of the root would produce 1 unit of nutrient.

In both cases, the maximum amount of nutrients that can reach the root node is .