Get Minimum Cost
Given an array arr of n integers, choose any two of the first three elements of the array and remove them. The cost of such an operation is the maximum of the two elements removed. For example, in the array [1, 2, 3, 4, 5], (1, 3) can be removed at a cost of 3, (1, 2) can be removed at a cost of 2 but (2, 4) cannot be removed as 4 is not amongst the first three elements of the array.
If there are less than three elements left in the array, remove all of them at a cost of the maximum of the remaining values.
Find the minimum cost to remove all the elements from the array.
Complete the function getMinCost in the editor.
getMinCost has the following parameter:
int arr[n]: an array of integers
Returns
int: the minimum cost to remove all the elements from the array
1Example 1
Among the possible options for the first step, i.e. {(3, 1), (1, 4), (3, 4)}, it is optimal to remove (3, 4) at a cost of 4. The remaining array in that case would be [1, 2]. You can now remove both the elements at a cost of 2. Thus the total cost to remove all the elements from the array is 6.
Removing any other combination in the first step, i.e. either of (3, 1) and (1, 4), would lead to a higher overall cost. For e.g. removing (3, 1) in the first step at a cost of 3 would entail removing (4, 2) in the next step at a cost of 4, leading to overall cost of 7.
Constraints
Limits and guarantees your solution can rely on.
1 ≤ n ≤ 10^31 ≤ arr[i] ≤ 10^6