The building of Saratov department of H&H company is situated at the beautiful place near the bank of the river Volga. In the building courtyard there is an ant hill, where ant known as Apache leaves.
The ant is very small and he wants to grow up as quickly as possible. There is an apple-tree nearby the ant hill, and Apache decided to eat all of the apples to come closer to his goal. All branches of the apple-tree grow up, and if branches were splitted once, they never grow together again. Each branch is finished either by a fork or by an apple.
The branches of the tree are pretty long, so ant's vigor quickly decreases while he is moving to the next apple. For each centimeter ant looses one point of energy of energy contained in this apple. Eating an apple increases ant's energy by certain amount. If energy level will fall below zero, Apache will die.
Your task is to help Apache to survive and find out how much energy he need to accumulate before the trip to successfully get all apples from the tree and return back. Note, ant can crawl along each branch only for two times.
The ant starts and finishes his movement at the beginning of the branch number one. The branch number one is considered as a stem of the tree.