Minimize the Sum of all the subarrays made up of the products of same | Python3 Program to implement the above approach ; Function to rearrange the second array such that the sum of its product of same indexed elements from both the arrays is minimized ; Stores ( i - 1 ) * ( n - i ) * a [ i ] for every i - th element ; Updating the value of pro according to the function ; Sort the array in reverse order ; Sort the products ; Updating the ans ; Return the ans ; Driver code ; Function call
mod = 1e9 + 7 NEW_LINE def findMinValue ( a , b ) : NEW_LINE INDENT n = len ( a ) NEW_LINE pro = [ 0 ] * ( n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT pro [ i ] = ( ( i + 1 ) * ( n - i ) ) NEW_LINE pro [ i ] *= ( a [ i ] ) NEW_LINE DEDENT b . sort ( reverse = True ) NEW_LINE pro . sort ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += ( pro [ i ] % mod * b [ i ] ) % mod NEW_LINE ans %= mod NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 ] NEW_LINE b = [ 2 , 3 , 2 ] NEW_LINE print ( int ( findMinValue ( a , b ) ) ) NEW_LINE DEDENT