Find the Maximum sum

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Find the Maximum sum相关的知识,希望对你有一定的参考价值。

Given an array of n elements.Find the maximum sum when the array elements will be arranged in such way. Multiply the elements of each pair and add to get maximum Sum. Sum could be larger so take mod with 10^9+7.

Example1: 
Input:  n=7
        -1,9,4,5,-4,-7,0
Output: 77
So to get the maximum sum,the arrangement will be {-7,-4},{-1,0},{9,5} and {4}.So the answer is (-7*(-4))+((-1)*0)+(9*5)+(4) ={77}.

Example2:
Input:  n=3
        -1,0,1
Output: 1
So to get the maximum sum,the arrangement will be {-1,0} and {1}.So the answer is ((-1)*0)+(1)={1}.

Input:
The first line consists of an integer T i.e number of test cases. The first line of each test case consists of an integer n.The next line consists of n spaced integers(positive or negative).

Output:
Print the maximum sum % 10^9+7.

Constraints: 
1<=T<=100
1<=n,a[i]<=10000

Example:
Input:
2
3
8 7 9
6
-1 9 4 5 -4 7

Output:
79
87

下面是我的代码实现:

 

以上是关于Find the Maximum sum的主要内容,如果未能解决你的问题,请参考以下文章