AppleOA #2
Element Swapping
估计中等评测数据准备中,可先阅读题面
考点(AI 标注)动规入门
A software development firm is hiring engineers and used the following challenge in its online test.
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
0 ≤ i < nand swaparr[i]andarr[j]. - 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] * (i + 1), using 0-based indexing. Find the maximum possible sum of the strength of all indices after optimal swaps. Mathematically, maximize the following: ∑_i=0^n-1 arr[i] * (i + 1) Example Consider n = 4,arr = [2, 1, 4, 3]. It is optimal to swap(arr[2], arr[3])and(arr[0], arr[1]). The final array is[1, 2, 3, 4]. The sum of strengths =(1 * 1 + 2 * 2 + 3 * 3 + 4 * 4) = 30, which is maximum possible. Thus, the answer is 30. Function Description Complete the functiongetMaximumSumOfStrengthsin the editor below.getMaximumSumOfStrengthshas the following parameter: int arr[n]:the initial array Returnslong int:the maximum possible sum of strengths of all indices after the operations are applied optimally
Constraints
1 ≤ n ≤ 10⁵1 ≤ arr[i] ≤ 10⁵