Remove the element

3.3

16 votes
Algorithms, Easy, Greedy Algorithms
Problem

You are given an integer array A of size N. You can perform an operation exactly N number of times. In the operation, you can remove one element from the array A and the operation will cost Ai units if ith is removed from the array. The operation will take 1 second.

Elements of array A will keep on increasing every second until it is removed. Let ith element is Ai at jth second. Then the ith element will increase by (j+1)Ai and the element will become Ai+(j+1)Ai at (j+1)th second.

Your task is to find the minimum cost to remove all the elements from the array A. As the answer can be very large, print the answer module 109+7.

Input Format
The first line of input contains a single integer T denoting the number of test cases.
The first line of each test case contains a single integer N denoting the number of elements in the array A.
The second line of each test case contains N space-separated integers, ith integer denotes Ai.

Output Format
Print the answer corresponding to each test case in new line.

Constraints

  • 1T10
  • 1N105
  • 1Ai109
Sample Input
1
2
3 3
Sample Output
9
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Initially, j=0
The cost of removing the first element is 3 and it will take 1 second.
After 1 second, the second element will become (j+1)A2+A2=13+3=6 and j will become 1.
The cost of removing second element will be 6.
Total cost = 3+6=9

Editor Image

?