Skip to content
Onsite

Permutation with Largest Sum

EasyP3Coding

You are given an array nums of n integers. You may rearrange it into any order. The value of an arrangement b is the sum of b[i] * i over all 0-based positions i.

Return the largest value you can get.

Example 1

Input
nums = [3,1,2]
Output
8
Why
Arrange as [1,2,3]: 1*0 + 2*1 + 3*2 = 8.

Example 2

Input
nums = [5]
Output
0

Example 3

Input
nums = [-2,4,1]
Output
9
Why
Arrange as [-2,1,4]: 0 + 1 + 8 = 9.

Constraints

  • 1 <= nums.length <= 1000
  • -10^4 <= nums[i] <= 10^4

Hints

Run checks the 3 visible tests, Submit all 9.