title: Can't figure out the optimal solution for this problem
body: <p>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.</p>
  <p>For example for the array [2, 1, 3, 2]:</p>
  <pre><code>0,0 -&gt; min(2) * sum(2) = 2 * 2 = 4  
  0,1 -&gt; min(2,1) * sum(2,1) = 1 * 3 = 3  
  0,2 -&gt; min(2,1,3) * sum(2,1,3) = 1 * 6 = 6  
  0,3 -&gt; min(2,1,3,4) * sum(2,1,3,2) = 1 * 8 = 8  
  1,1 -&gt; min(1) * sum(1) = 1 * 1 = 1  
  1,2 -&gt; min(1,3) * sum(1,3) = 1 * 4 = 4  
  1,3 -&gt; min(1,3,2) * sum(1,3,2) = 1 * 6 = 6  
  2,2 -&gt; min(3) * sum(3) = 3 * 3 = 9  
  2,3 -&gt; min(3,2) * sum(3,2) = 2 * 5 = 10  
  3,3 -&gt; min(2) * sum(2) = 2 * 2 = 4  
  Total = 55  
  </code></pre>
  <p>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.</p>
  <pre><code>public static int func(int[] nums) {
      int total = 0;
  
      for (int i = 0; i &lt; nums.length; i++) {
          int min = nums[i], sum = nums[i];
          total += min * sum;
  
          for (int j = i+1; j &lt; nums.length; j++) {
              min = Math.min(min, nums[j]);
              sum += nums[j];
              total += min * sum;
          }
      }
  
      return total;
  }
  </code></pre>