Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Mandragora Forest/mandragora_forest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#HackerRank Problem Algorithms - Dynamic Programming


def maxExp(a, size):
# if no mandragoras is eaten s = 1 and experience = sum(a)
max_exp = curr_exp = sum_of_arr = sum(a)

for i in range(size):
# update the sum_of_arr by removing the value mandragoras eaten
sum_of_arr -= a[i]
# formula for experience after eating all mandragoras from 0 to i th position and fighting the remaining
curr_exp = (curr_exp - (a[i] * (i+1))) + sum_of_arr
max_exp = max(curr_exp, max_exp)

return max_exp


testcases = int(input())

for t in range(testcases):
n = int(input())
arr = list(map(int, input().split()))
arr = sorted(arr) # sort the array
print(maxExp(arr, n))