import sys
n, k = [int(component) for component in input().split(" ")]
heights = list(map(int,sys.stdin.readline().split()))
max_from_left = [0] * n
max_from_right = [0] * n
enforced = 0
for i in range(n):
if heights[i] <= enforced:
max_from_left[i] = enforced
else:
max_from_left[i] = heights[i]
enforced = heights[i]
enforced -= k
enforced = 0
for i in range(n-1, -1, -1):
if heights[i] <= enforced:
max_from_right[i] = enforced
else:
max_from_right[i] = heights[i]
enforced = heights[i]
enforced -= k
ans = sum(max(max_from_left[i], max_from_right[i])-heights[i] for i in range(n))
print(ans)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import sys n, k = [int(component) for component in input().split(" ")] heights = list(map(int,sys.stdin.readline().split())) max_from_left = [0] * n max_from_right = [0] * n enforced = 0 for i in range(n): if heights[i] <= enforced: max_from_left[i] = enforced else: max_from_left[i] = heights[i] enforced = heights[i] enforced -= k enforced = 0 for i in range(n-1, -1, -1): if heights[i] <= enforced: max_from_right[i] = enforced else: max_from_right[i] = heights[i] enforced = heights[i] enforced -= k ans = sum(max(max_from_left[i], max_from_right[i])-heights[i] for i in range(n)) print(ans) |
English