Running sum of k largest

Write codeMedium15 min

Your task

Integers arrive one at a time from a stream. After each arrival, report the sum of the k largest values seen so far; while fewer than k values have arrived, report the sum of all of them. Return one total per arrival, in order. Values may repeat and may be negative. Example: nums = [3, 1, 5, 2], k = 2 gives [3, 4, 8, 8], after the third arrival the two largest are 5 and 3, and the final 2 changes nothing.

Examples

running_top_k_sum(nums: list[int], k: int) -> list[int]
Example 1
Inputrunning_top_k_sum([3, 1, 5, 2], 2)
Output[3, 4, 8, 8]