How to find the maximum value we can achieve by picking k elements from a list in Python

1 Answer

0 votes
def max_sum_of_k(arr, k):
    arr_sorted = sorted(arr, reverse=True)
    return sum(arr_sorted[:k])

arr = [11, 2, 4, 9, 3, 6, 5, 1]
k = 3

print(max_sum_of_k(arr, k))  



'''
run:

26

'''

 



answered Apr 6 by avibootz

Related questions

...