Timer is Running

Given an array **A** of **N** integers. Your task is to write a program to find the maximum value of **∑arr[i]*i**, where i = 0, 1, 2,…., n – 1.

You are allowed to rearrange the elements of the array.

**Note**: Since output could be large, hence module 10^{9}+7 and then print answer.

**Input:**

First line of the input contains an integer **T**, denoting the number of test cases. Then **T** test case follows. Each testcase contains two lines of input. First line contains an integer **N**, denoting the size of the array. Next line contains **N **space separated integers denoting the elements of the array.

**Output:**

For each test case, print the required answer on a new line.

**Constraints:**

1 <= T <= 10^{3}

1 <= N <= 10^{7}

1 <= A_{i} <= N

**Example:
Input:**

2

5

5 3 2 4 1

3

1 2 3

40

8

**Explanation:**

**Testcase1:** If we arrange the array as 1 2 3 4 5 then we can see that the minimum index will multiply with minimum number and maximum index will multiply with maximum number. So 1*0+2*1+3*2+4*3+5*4=0+2+6+12+20 =40 mod(10^{9}+7) = 40

Author: harshitsidhwa

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

sandeep28august | 642 |

sudhanmadhu7981 | 631 |

RudraKaniya | 624 |

ishaanprajapati808 | 620 |

saxenadivyanshi004 | 572 |

sandeep28august | 642 |

sudhanmadhu7981 | 631 |

RudraKaniya | 629 |

ishaanprajapati808 | 620 |

saxenadivyanshi004 | 572 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4736 |

Login to report an issue on this page.