application. The goal is to have the computational
power of the servers in non-decreasing order. To do
50, you can increase the computational power of
each server in any contiguous segment by x.
Choose the values of x such that after the
computational powers are in non-decreasing order,
the sum of the x values is minimum.
Example
There are n = 5 servers and their computational
power =[3, 4, 1, 6, 2].
Add 3 units to the subarray (2, 4) and 4 units to the
subarray (4, 4). The final arrangement of the servers
is:[3,4,4,9,9]. Theansweris3+4=7.
def makePowerNonDecreasing(power):
n = len(power)
total_sum = 0
for i in range(n - 1):
if power[i] > power[i + 1]:
x = power[i] - power[i + 1]
for j in range(i + 1, n):
power[j] += x
total_sum += x
return total_sum
computational_power = [3, 2, 1]
result = makePowerNonDecreasing(computational_power)
print(result)