Problems > Sum over All Subarrays

Sum over All Subarrays

Difficulty: 1500 3000 ms 128 MB

math prefix-sums

Add up the sums of every contiguous subarray and print the grand total.

Input: the first line has N; the second line has N integers.

Output: the total of all subarray sums.

Constraints:

  • 1 ≤ N ≤ 105
  • −104 ≤ ai ≤ 104

Sample Tests

Input 1
3
1 2 3
Output 1
20
Input 2
2
-1 5
Output 2
8

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.