#K57532. Sum of All Subarrays

    ID: 30441 Type: Default 1000ms 256MiB

Sum of All Subarrays

Sum of All Subarrays

Given an array of integers, compute the cumulative sum of all subarray sums. A subarray is a contiguous segment of the array. In other words, for an array \(A = [a_1, a_2, \ldots, a_n]\), you need to compute:

[ \text{Answer} = \sum_{i=1}^{n} \sum_{j=i}^{n} \left(\sum_{k=i}^{j} a_k\right) ]

This can be efficiently calculated by noting that the contribution of \(a_i\) to the final sum is \(a_i \cdot i \cdot (n-i+1)\). Use this property to compute the answer.

inputFormat

The first line contains a single integer (n) — the number of elements in the array. The second line contains (n) space-separated integers representing the array elements.

outputFormat

Output a single integer representing the cumulative sum of all subarray sums.## sample

1
1
1