Loading Now

calculateMaxMinusMinOfAllSubsequence1

Given an array a with n elements.
A subsequence [l,r] (1 ≤ l ≤ r ≤ n) of  a is a sequence whose elements are al, al+1, … , ar.
u is the largest total element of the subsequece.
v is the smallest total number of subsequece.
Calculate u-v.

Example:

  • For a={2, 1, 4}the output should be calculateMaxMinusMinOfAllSubsequence(a)=7. Explain:
    u=max[1,1]+max[1,2]+max[1,3]+max[2,2]+max[2,3]+max[3,3]
    =2+2+4+1+4+4
    =17
    v=min[1,1]+min[1,2]+min[1,3]+min[2,2]+min[2,3]+min[3,3]
    =2+1+1+1+1+4
    =10
    u-v=17-10=7

Input/Output:

  • [Execution time limit] 0.5s in C++, 3s in Java and C#, 4s in Python, GO and Js

  • [Input] integer.arrays a
    1 <= a.size() <= 103
    -10<= a[i] <= 109

  • [Output] long
    Calculate u-v.

Post Comment

Contact