Given an integer array A of size N. In one operation, you can remove any element from the array, and the cost of this operation is the sum of all elements in the array present before this operation.
Find the minimum cost to remove all elements from the array.
A = [2, 1]
- Given array A = [2, 1]
- Remove 2 from the array => [1]. Cost of this operation is (2 + 1) = 3.
- Remove 1 from the array => []. Cost of this operation is (1) = 1. So, total cost is = 3 + 1 = 4.
A = [5]
- There is only one element in the array. So, cost of removing is 5.
Code is below
def solve(self, A):
A.sort()
if len(A) is 1:
return A[0]
sm = sum(A)
for each in A:
A.pop()
sm += sum(A)
return sm
I am not getting proper out. For small arrays [8,0,10 ] i got 26 which is correct output, but sometimes it fails where duplicate numbers
A = [ 579, 407, 436, 847, 929, 430, 40, 730, 608, 710, 796, 722, 48, 514, 582, 858, 634, 303, 292, 323, 869, 442, 754, 247, 10, 551, 383, 523, 878, 931, 970 ]
Here my output is 180350 The expected returned value : 204428