码迷,mamicode.com
首页 > 其他好文 > 详细

Find the Maximum sum

时间:2017-10-16 16:55:28      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:traints   positive   out   aced   nsis   number   pac   air   case   

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

标签:traints   positive   out   aced   nsis   number   pac   air   case   

原文地址:http://www.cnblogs.com/wongyi/p/7676839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!