'Can't figure out the optimal solution for this problem

Given an array of integers, for each consecutive subarray, multiply the minimum value in the subarray to the sum of all the values in the subarray. Return the sum of all the products that are calculated.

For example for the array [2, 1, 3, 2]:

0,0 -> min(2) * sum(2) = 2 * 2 = 4  
0,1 -> min(2,1) * sum(2,1) = 1 * 3 = 3  
0,2 -> min(2,1,3) * sum(2,1,3) = 1 * 6 = 6  
0,3 -> min(2,1,3,4) * sum(2,1,3,2) = 1 * 8 = 8  
1,1 -> min(1) * sum(1) = 1 * 1 = 1  
1,2 -> min(1,3) * sum(1,3) = 1 * 4 = 4  
1,3 -> min(1,3,2) * sum(1,3,2) = 1 * 6 = 6  
2,2 -> min(3) * sum(3) = 3 * 3 = 9  
2,3 -> min(3,2) * sum(3,2) = 2 * 5 = 10  
3,3 -> min(2) * sum(2) = 2 * 2 = 4  
Total = 55  

This is the solution I was able to come up with however it is not fast enough and I can't figure out a faster solution.

public static int func(int[] nums) {
    int total = 0;

    for (int i = 0; i < nums.length; i++) {
        int min = nums[i], sum = nums[i];
        total += min * sum;

        for (int j = i+1; j < nums.length; j++) {
            min = Math.min(min, nums[j]);
            sum += nums[j];
            total += min * sum;
        }
    }

    return total;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source