# Program to get maximum value of power of a list by rearranging elements in Python

Suppose we have a list nums of N positive numbers. Now we can select any single value from the list, and move (not swap) it to any position. We can also not move any to position at all. So we have to find what is the maximum possible final power of the list? As we know the power of a list is the sum of (index + 1) * value_at_index over all indices i.

$$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$

So, if the input is like nums = [6, 2, 3], then the output will be 26, as we can move the 6 to the end to get list [2, 3, 6] so the power is: (2 * 1) + (3 * 2) + (6 * 3) = 26.

To solve this, we will follow these steps −

• P := a list with value 0

• base := 0

• for each index i and value x of A, do

• insert last element of P + x at the end of P

• base := base + (i+1) * x

• ans := base

• for each index i and value x in A, do

• for j in range 0 to size of A + 1, do

• ans := maximum of ans and (base + P[i] - P[j] -(i - j) * x)

• return ans

## Example

Let us see the following implementation to get a better understanding −

Live Demo

class Solution:
def solve(self, A):
P = [0]
base = 0
for i, x in enumerate(A, 1):
P.append(P[-1] + x)
base += i * x
ans = base
for i, x in enumerate(A):
for j in range(len(A) + 1):
ans = max(ans, base + P[i] - P[j] - (i - j) * x)
return ans
ob = Solution()
nums = [6, 2, 3]
print(ob.solve(nums))

## Input

[6, 2, 3]

## Output

26