Quite interesing problem. We need to minize function of sum_l sum_r f(l,r)
, where f(l,r) = sum_i ai * bi, i<-[l,r]
.
So first of all forget about the fact we have two vectors, try to rewrite sum of all the sum of subarrys in one sum.
Should have a form sum_i ai * coef(i, n)
.
How many subarrays are in the array of length n? The same is how many ordered pairs <i,j>
?
For each i
there (n - i)
subarrays starting at this i
.
The total number is S(n) = sum_i (n - i) = n^2 - n(n-1)/2 = n(n+1)/2
.