A depot offers delivery slots at n distinct integer times. Starting from an empty binary search tree, insert the slot times in the order given, using the standard BST rule: to insert a key x, walk down from the root, going left if x is smaller than the current node and right if x is larger, until an empty spot is reached, where a new node is created. No rebalancing is performed.
For each of q requested times t, the driver is assigned the floor slot: the largest existing slot time that is less than or equal to t. The shortfall of that request is t - floor. If no slot time is less than or equal to t, that request is unreachable and contributes 0 to the total (it is not counted as -1).
Report the sum of shortfalls over all q requests.
Line 1: an integer n.
Line 2: n space-separated distinct integers — the slot times, in insertion order.
Line 3: an integer q.
Line 4: q space-separated integers t_1 ... t_q — the requested times.
A single integer: the total shortfall summed across all requests.
Example 1
Input
5 5 3 8 1 4 3 4 6 0
Expected
1
Explanation
Slots are {1,3,4,5,8}. Floor of 4 is 4 (shortfall 0). Floor of 6 is 5 (shortfall 1). No slot is <= 0, so that request contributes 0. Total = 0 + 1 + 0 = 1.
Example 2
Input
4 10 20 5 15 2 12 3
Expected
2
Explanation
Slots are {5,10,15,20}. Floor of 12 is 10 (shortfall 2). No slot is <= 3 (the minimum slot is 5), so that request contributes 0. Total = 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →