def main():
n, k = map(int, input().split())
path = list(map(int, input().split()))
# print(path)
counter = 0
changes = True
while changes:
changes = False
for i in range(1, n):
if abs(path[i] - path[i-1]) <= k:
continue
changes = True
if path[i] > path[i-1]:
to_be_added = path[i] - path[i-1] - k
counter += to_be_added
path[i-1] += to_be_added
else:
to_be_added = path[i-1] - path[i] - k
counter += to_be_added
path[i] += to_be_added
# print(path)
print(counter)
if __name__ == "__main__":
main()
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 29 30 | def main(): n, k = map(int, input().split()) path = list(map(int, input().split())) # print(path) counter = 0 changes = True while changes: changes = False for i in range(1, n): if abs(path[i] - path[i-1]) <= k: continue changes = True if path[i] > path[i-1]: to_be_added = path[i] - path[i-1] - k counter += to_be_added path[i-1] += to_be_added else: to_be_added = path[i-1] - path[i] - k counter += to_be_added path[i] += to_be_added # print(path) print(counter) if __name__ == "__main__": main() |
English