Description
Solutions
Get Maximum Sum of Strengths
🔥 FULLTIME

Given an array arr that contains n integers, the following operation can be performed on it any number of times (possibly zero):

Choose any index 1 ≤ i ≤ n and swap arr[i]. Each element of the array can be swapped at most once during the whole process.

The strength of an index is defined as (arr[i] + 1) using 1-based indexing. Find the maximum possible sum of the strength of all indices after optimal swaps. Mathematically, maximize the following:

Function Description

Complete the function getMaximumSumOfStrengths in the editor below.

getMaximumSumOfStrengths has the following parameter:

  1. int arr[n]: the initial array

Returns

long int: the maximum sum of strengths

Example 1:

Input:  arr = [1, 9, 7, 3, 2]
Output: 66
Explanation:

It is optimal to swap (arr[2], arr[3]). The final array is arr[] = [1, 9, 3, 7, 2]. The sum of strengths (1*1 + 2*9 + 3*3 + 4*7 + 5*2) = 66, which is the maximum possible.

Example 2:

Input:  arr = [2, 1, 4, 3]
Output: 30
Explanation:

It is optimal to swap (arr[0], arr[1]) and (arr[2], arr[3]). The final array is arr[] = [1, 2, 3, 4]. The sum of strengths (1*1 + 2*2 + 3*3 + 4*4) = 30, which is the maximum possible.

Example 3:

Input:  arr = [1, 2, 5]
Output: 20
Explanation:

No swaps are needed as the array is already in optimal order. The sum of strengths (1*1 + 2*2 + 3*5) = 20, which is the maximum possible.

Constraints:
  • 1 ≤ n ≤ 10^5
  • 1 ≤ arr[i] ≤ 10^5
Thumbnail 0
Thumbnail 1
Testcase

Result
Case 1

input:

output: