HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Sections > Divide&Conquer and Binary Search Trees (BST) > problem:


50805 - Sum of the weights in a BST

Guest
• Review clarifications (1)

Section problems

• 50770 - Average Depth
• 50411 - Sum of the Leaves
• 50830 - Sorting BST Nodes
• 50803 - Sum of the depths in BST
• 50805 - Sum of the weights in a...
• 50818 - Depth Limited BST
• 50836 - Censored
• 50816 - Largest Sum Path
• 50772 - The Path of a Node
• 50863 - Total Access Cost of a BST
• 50771 - BST Level Sum
• 50773 - Balanced Sum Tree
• 50722 - Scientist's problem

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