#K52637. Sum of All Subarrays
Sum of All Subarrays
Sum of All Subarrays
You are given an array of integers and your task is to compute the total sum of all subarrays. For a given array A of length n, an element A[i] appears in exactly ( (i+1) \times (n-i) ) subarrays. Hence, its total contribution is ( A[i] \times (i+1) \times (n-i) ). The overall answer is the sum of the contributions of all elements.
For example, consider the array [1, 2, 3]. The contributions are:
1 appears in 13 = 3 subarrays,
2 appears in 22 = 4 subarrays, and
3 appears in 31 = 3 subarrays.
The sum of all contributions is 13 + 24 + 33 = 20.
Input consists of multiple test cases. For each test case, you are given the size of the array and then the array elements.
inputFormat
The input is read from standard input (stdin). The first line contains an integer T, the number of test cases. For each test case:
1. The first line contains an integer n, the size of the array.
2. The second line contains n space-separated integers representing the array elements.
outputFormat
For each test case, output a single line to standard output (stdout) containing the total sum of all subarrays for the given array.## sample
1
3
1 2 3
20
</p>