HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Volumes > Data Structures > problem:


50805 - Sum of the weights in a BST

Guest
• Review clarifications (1)

Volume problems

• 50677 - The Cottage
• 50795 - Trunk
• 50414 - Traffic
• 50698 - Ayran Delivery
• 50978 - Albanian Coders
• 50699 - Fighting Vampires
• 50830 - Sorting BST Nodes
• 50803 - Sum of the depths in BST
• 50805 - Sum of the weights in a...
• 50712 - Moon algebra
• 50675 - Kruja Boys
• 50415 - The Scientist
• 50679 - Jetpack Hurdle Jumping
• 50717 - Hurdle Jumping
• 50421 - Repairing road segments
• 50431 - Sultan's Game
• 50429 - Secret Message

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.
Question by Sidrit Reka.

Sum of the weights in a BST

Question: You will be given a series of numbers to be inserted into an initially empty Binary Search Tree (BST). Each of the numbers will also contain a certain weight. Calculate the sum of the weights of all nodes found below a given node.

Input specification
You will be first given a number (n) the number of nodes to be inserted in the BST where 1 <= n <= 10000. Then you will be given in n lines, a pair of integers (id, weight) where id represents the number of the node and weight represents its weight where id 1 <= id, weight <= 10000. Then in the end you will be given the id of the node for which you have to calculate the sum of the weights of all nodes found below it.

Output specification
Show one integer which represents the sum of the weights of all nodes found below the given node. In case the given node is not found in the BST, just print 0.

Sample Input I
6
4 3
7 6
3 7
2 2
6 4
3 4
4
Sample Output I
19

We want to find the sum of the weights below node nr. 4. Below node 4 we have 4 other nodes (3, 2, 7, 6). We take the weight of each of these nodes and we sum them (7 + 2 + 6 + 4 = 19).

Sample Input II
9
6 3
8 4
2 2
4 1
1 6
7 3
9 2
3 5
5 1
2
Sample Output II
13

We want to find the sum of the weights below node nr. 2. Below node 2 we have 4 other nodes (1, 4, 3 ,5). We take the weight of each of these nodes and we sum them (6 + 1 + 5 + 1 = 13).



Для отправки решений необходимо выполнить вход.

www.contester.ru