MillenniumOA #3
Max Sum of Strengths after At-Most-One Adjacent Swap (per index)
Estimated MediumJudge-ready
You are given an array of positive integers. You may choose several pairwise non-adjacent index pairs (i,i+1) and swap their elements, or make no swaps at all. Each element takes part in at most one swap. Indices start at 0, and after the swaps the strength of the array is Σ arr[i]×(i+1). Find the maximum strength.
This site added a standard input/output protocol.
Input
The first line contains n (1 ≤ n ≤ 100000), and the second line contains n integers arr[i] (1 ≤ arr[i] ≤ 100000).
Output
Output the maximum achievable strength sum.
Example 1
Input:
5
1 9 7 3 2
Output:
66
Example 2
Input:
3
3 2 1
Output:
11
Example 3
Input:
1
1
Output:
1
Editorial
Loading solution…