You are given an array nums of length n. For every index i, you create a subarray that ends at index i, but the starting position depends on the value at index i.
Specifically:
So the subarray considered is:
You must find the sum of all elements from all such subarrays for every i in the array and return the total sum.
Input: nums = [1,2,2]
Output: 8
Explanation:
i Subarray Sum 0 [1] 1 1 [1,2] 3 2 [1,2,2] 5 Total Sum = 1 + 3 + 5 = 8
Input: nums = [4,0,3]
Output: 10
Explanation:
i Subarray Sum 0 [4] 4 1 [0] 0 2 [0,3] 3 Total Sum = 4 + 0 + 3 = 7
Accepted:
Submission: