def solve(step,k,n):
sc = 0
i = 0
#print(step)
while i < n-1:
#print("STEP",i)
#print(":",step)
r = abs(step[i] - step[i+1])
if r <= k: #jest w bound
#print("!",r,k)
i+=1
continue
#wymaga korekty
sc+=r-k
if step[i] >= step[i+1]: #koryguje się nastepny
step[i+1] += r-k
#print("/step forward:",step)
i += 1
continue
else: #trzeba zawrócic
step[i] += r-k
#print("/step back:",step)
if i == 0:
continue
i -= 1
continue
#print("!!",step)
return sc
n,k = [int(x) for x in input().split()]
step = [int(x) for x in input().split()]
sc = solve(step,k,n)
print(sc)
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 31 32 33 34 | def solve(step,k,n): sc = 0 i = 0 #print(step) while i < n-1: #print("STEP",i) #print(":",step) r = abs(step[i] - step[i+1]) if r <= k: #jest w bound #print("!",r,k) i+=1 continue #wymaga korekty sc+=r-k if step[i] >= step[i+1]: #koryguje się nastepny step[i+1] += r-k #print("/step forward:",step) i += 1 continue else: #trzeba zawrócic step[i] += r-k #print("/step back:",step) if i == 0: continue i -= 1 continue #print("!!",step) return sc n,k = [int(x) for x in input().split()] step = [int(x) for x in input().split()] sc = solve(step,k,n) print(sc) |
English