Sum of k largest

Write codeEasy10 min

Your task

Given a list of integers and an integer k, return the sum of the k largest values. Values may repeat, and each occurrence counts on its own. If k is greater than the length of the list, sum every element; if k is zero or the list is empty, return 0. Negative numbers are allowed. Example: nums = [4, 1, 7, 3], k = 2 gives 11 because the two largest values are 7 and 4.

Examples

sum_of_k_largest(nums: list[int], k: int) -> int
Example 1
Inputsum_of_k_largest([4, 1, 7, 3], 2)
Output11