Maximize Power

0

0 votes
Geometry, Dynamic Programming
Problem

Chuchu has got an array A of n intergers and he want to maximize the power of the array. Power of an array is defined as, 

  • Power(A)=ni=1iAi

In order to maximize the power, he is allowed to do following operation at most once,

  • Pick any element from the given array and insert it at any other position.(While inserting, some elements of array get shifted accordingly). 

Note: Given array A is 1-indexed.

Input:

  • First line contains a integer n, denoting size of array.
  • Second line contains  n space separated integers, each denoting Ai.

Output:

Print the maximum power of array Chuchu can obtain after performing given operation at most once.

Constraints:

  • 1n200000
  • 0|Ai|106
Sample Input
5
1 1 2 7 1
Sample Output
49
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For given test case, before performing any operation Power(A)=42.

After rotating subarray [2,7,1] right by 1 element, A becomes [1,1,1,2,7], now Power(A)=49.  

Editor Image

?