This page is still under construction.

Parts of this page are still being built. What you see may change.

Nutrient Tree

Time limit2sMemory limit512 MB

Summary
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 ll leaves (1≤l≤501 \le l \le 50). Each leaf kk produces an amount of nutrients nkn_k (1≤nk≤100001 \le n_k \le 10000).

Every branch of the tree (think of it as an edge) limits how many nutrients can flow along it toward the root. You have XX growth agents (1≤X≤25001 \le X \le 2500), and each agent can be spent in one of two ways:

  • Thicken an edge. Every edge starts with weight 11. If you assign ww growth agents to an edge, that edge can carry at most (1+w)2(1 + w)^2 nutrients.
  • Boost a leaf. If you assign ss growth agents to a leaf whose initial value is nkn_k, its production becomes nk+sn_k + s.

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 nkn_k (1≤nk≤100001 \le n_k \le 10000) denotes a single leaf that produces nkn_k nutrients, or
  • (TL TR)(T_L\ T_R) denotes an internal node whose left and right subtrees are described by TLT_L and TRT_R (the two subtree descriptions are separated by a space and wrapped in parentheses).

The second line contains the integer XX (1≤X≤25001 \le X \le 2500), 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.

Examples2

  1. Example 1

    Input
    (5 ((7 1) (3 4)))
    3
    
    Expected output
    7
    
  2. Example 2

    Input
    (1 1)
    2
    
    Expected output
    3