Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from fractions import gcd
from math import factorial, ceil, sqrt, atan2, log, pi, e, asin,acos, cos, sin
from itertools import *
from fractions import Fraction
import string
import copy
import random
import bisect
from decimal import *
def id_generator(size=20, chars=string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
 
def mp():
    return map(int,str(raw_input()).split())

a,b=mp()
if a>b:
    if gcd(a,b)==1:
    	print a
	elif a%b==0:
		print -1
	else:
		print a
else:
	if gcd(a,b)==1:
    	print b
	elif b%a==0:
		print -1
	else:
		print b

Fixed submission:
from fractions import gcd
from math import factorial, ceil, sqrt, atan2, log, pi, e, asin,acos, cos, sin
from itertools import *
from fractions import Fraction
import string
import copy
import random
import bisect
from decimal import *
def id_generator(size=20, chars=string.digits):
    return ''.join(random.choice(chars) for _ in range(size))
 
def mp():
    return map(int,str(raw_input()).split())
 
 
a,b=mp()
if a>=b:
    if a%b==0:
    	print -1
    else:
    	print a
else:
	if 0:
		print -1
	else:
		print a
Buggy python submission (verdict: Time Limit Exceeded):

""""
5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3
"""

N, M = map(int, raw_input().split())

G = []
for _ in xrange(M):
    G.append(map(int, raw_input().split()))

h = [0]*(N+1)

for eg in G:
    h[eg[1]]+=1

que = []

for i in xrange(1, N+1):
    if h[i] == 0: que.append(i)

dp = [0]*(N+1)

while len(que)>0:
    now = que.pop(0)
    for eg in G:
        if eg[0]==now:
            h[eg[1]]-=1
            if h[eg[1]]==0:
                que.append(eg[1])
                dp[eg[1]] = max(dp[eg[0]] + 1, dp[eg[1]])

print max(dp)

Fixed submission:

""""
5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3
"""

N, M = map(int, raw_input().split())

G = []
for _ in xrange(M):
    G.append(map(int, raw_input().split()))

h = [0]*(N+1)

dest = [[] for _ in xrange(N+1)]

for eg in G:
    h[eg[1]]+=1
    dest[eg[0]].append(eg[1])

que = []

for i in xrange(1, N+1):
    if h[i] == 0: que.append(i)

dp = [0]*(N+1)

while len(que)>0:
    now = que.pop(0)
    for edest in dest[now]:
        h[edest]-=1
        if h[edest]==0:
            que.append(edest)
            dp[edest] = max(dp[now] + 1, dp[edest])

print max(dp)
Buggy python submission (verdict: Runtime Error):

n, k = map(int, input().split())
h_list = list(map(int, input().split()))

dp = [1000000] * n
dp[0] = 0

for i in range(1, n):
    for j in range(i-1, max(0, i-k) - 1, -1):
        dp[i] = min(dp[j] + abs(h_list[i] - h_list[j]), dp[i])

print(dp[n-1])


Fixed submission:

n = int(input())

h_list = [int(h) for h in input().split()]

dp = [0] * n
dp[0], dp[1] = 0, abs(h_list[1] - h_list[0])

for i in range(2, n):
    dp[i] = min(dp[i-1] + abs(h_list[i] - h_list[i-1]),
                dp[i-2] + abs(h_list[i] - h_list[i-2]))

print(dp[n-1])
Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == '__main__':
    N = int(input())
    S = input()

    min = N
    for i in range(N):
        score = S[0:i].count('W') + S[i+1:N].count('E')
        if min > score:
            min = score

    print(min)

Fixed submission:
if __name__ == '__main__':
    N = int(input())
    S = input()

    old = S[1:].count('E')
    ans = old

    for i in range(1, N):
        if S[i-1] == 'W' and S[i] == 'W':
            new = old + 1
        elif S[i-1] == 'E' and S[i] == 'E':
            new = old - 1
        else:
            new = old

        ans = min(ans, new)
        old = new

    print(ans)
Buggy python submission (verdict: Runtime Error):
from typing import Union, List
import sys
from copy import copy

sys.setrecursionlimit(10**5)

# 木の根を返す
def find(x: int, S: List[int]) -> int:
    if S[x] == x:
        return x
    else:
        S[x] = find(S[x], S)
        return S[x]

# xとyが同じ集合に属すかの判定
def same(x: int, y: int, S: List[int]) -> bool:
    return find(x, S) == find(y, S)

# xとyの属する集合を結合
def unite(x: int, y: int, S: List[int]) -> None:
    rx = find(x, S)
    ry = find(y, S)
    if rx == ry:
        return None
    else:
        S[rx] = ry

if __name__ == '__main__':
    [N, M] = [int(i) for i in input().split()]

    S = [int(i) for i in range(N+1)]
    p = [0] + [int(num) for num in input().split()]

    for i in range(M):
        [x, y] = [int(i) for i in input().split()]
        unite(x, y, S)

    score = 0
    for i in range(1, N+1):
        if same(i, p[i], S):
            score += 1
    print(score)


Fixed submission:
import sys
from copy import copy

sys.setrecursionlimit(10**5)

# 木の根を返す
def find(x, S):
    if S[x] == x:
        return x
    else:
        S[x] = find(S[x], S)
        return S[x]

# xとyが同じ集合に属すかの判定
def same(x, y, S):
    return find(x, S) == find(y, S)

# xとyの属する集合を結合
def unite(x, y, S):
    rx = find(x, S)
    ry = find(y, S)
    if rx == ry:
        return None
    else:
        S[rx] = ry

if __name__ == '__main__':
    [N, M] = [int(i) for i in input().split()]

    S = [int(i) for i in range(N+1)]
    p = [0] + [int(num) for num in input().split()]

    for i in range(M):
        [x, y] = [int(i) for i in input().split()]
        unite(x, y, S)

    score = 0
    for i in range(1, N+1):
        if same(i, p[i], S):
            score += 1
    print(score)
Buggy python submission (verdict: Runtime Error):
def main():
    [N, M] = [int(i) for i in input().split()]

    E = []

    for i in range(M):
        E.append([int(i) for i in input().split()])

    dist = [-1*10**100 for i in range(N)]
    dist[0] = 0

    for i in range(N):
        for [a, b, c] in E:
            newLen = dist[a-1] + c
            if newLen > dist[b-1]:
                dist[b-1] = newLen

    dist2 = dist.copy()

    for i in range(N):
        for [a, b, c] in E:
            newLen = dist2[a-1] + c
            if newLen > dist2[b-1]:
                dist2[b-1] = newLen

    if dist != dist2:
        print('inf')
    else:
        print(dist[N-1])


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    [N, M] = [int(i) for i in input().split()]
    edge = [[10**100] * N for _ in range(N)]

    E = []

    for i in range(M):
        E.append([int(i) for i in input().split()])

    dist = [-1*10**100 for i in range(N)]
    dist[0] = 0

    flag = False

    for i in range(N):
        for [a, b, c] in E:
            newLen = dist[a-1] + c
            if newLen > dist[b-1]:
                dist[b-1] = newLen
        if i == N-1 and dist[N-1] > predist:
            flag = True
        predist = dist[N-1]

    if flag:
        print('inf')
    else:
        print(dist[N-1])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    [N, M] = [int(i) for i in input().split()]

    E = []

    for i in range(M):
        E.append([int(i) for i in input().split()])

    dist = [-1*10**100 for i in range(N)]
    dist[0] = 0

    for i in range(N):
        for [a, b, c] in E:
            newLen = dist[a-1] + c
            if newLen > dist[b-1]:
                dist[b-1] = newLen

    dist2 = dist.copy()

    for i in range(N):
        for [a, b, c] in E:
            newLen = dist[a-1] + c
            if newLen > dist[b-1]:
                dist2[b-1] = newLen

    if dist != dist2:
        print('inf')
    else:
        print(dist[N-1])


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    [N, M] = [int(i) for i in input().split()]
    edge = [[10**100] * N for _ in range(N)]

    E = []

    for i in range(M):
        E.append([int(i) for i in input().split()])

    dist = [-1*10**100 for i in range(N)]
    dist[0] = 0

    flag = False

    for i in range(N):
        for [a, b, c] in E:
            newLen = dist[a-1] + c
            if newLen > dist[b-1]:
                dist[b-1] = newLen
        if i == N-1 and dist[N-1] > predist:
            flag = True
        predist = dist[N-1]

    if flag:
        print('inf')
    else:
        print(dist[N-1])


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
if __name__ == '__main__':
    [N, C, K] = [int(i) for i in input().split()]

    T = []
    for i in range(K):
        T.append(int(input()))
    T.sort()

    counter = 1

    num_people = 0

    flag = False

    for num in T:
        print(num)
        print(flag)
        num_people += 1
        if flag:
            if num - first_time <= K and num_people <= C:
                continue
            else:
                counter += 1
                num_people = 1
                flag = False
                print('inc')
        else:
            first_time = num
            flag = True
        

    print(counter)


Fixed submission:
if __name__ == '__main__':
    [N, C, K] = [int(i) for i in input().split()]

    T = []
    for i in range(N):
        T.append(int(input()))
    T.sort()

    counter = 1

    num_people = 0

    flag = False

    for num in T:
        num_people += 1
        if flag:
            if num - first_time <= K and num_people <= C:
                continue
            else:
                counter += 1
                num_people = 1
                first_time = num

        else:
            first_time = num
            flag = True

    print(counter)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

k = min(n-1, k)

inf = 100000000

dp = [inf for _ in [0]*n]

for i in range(k+1):
	dp[i] = abs(h[0] - h[i])

for i in range(k+1, n):
	
	dp[i] = min([dp[i-j] + abs(h[i-j] - h[i]) for j in range(1, k+1)])

print(dp[-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

k = min(n-1, k)

inf = 100000000

dp = [inf for _ in [0]*n]

def cost(i, j):
	return abs(h[i] - h[j])

for i in range(k+1):
	dp[i] = cost(0, i)

for i in range(k+1, n):
	
	dp[i] = min([dp[i-j] + cost(i-j, i) for j in range(1, k+1)])

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
from functools import lru_cache

n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

k = min(n-1, k)

inf = 100000000

dp = [inf for _ in [0]*n]

@lru_cache(maxsize=None)
def cost(i, j):
	return abs(h[i] - h[j])

for i in range(k+1):
	dp[i] = cost(0, i)

for i in range(k+1, n):
	
	dp[i] = min([dp[i-j] + cost(i-j, i) for j in range(1, k+1)])

print(dp[-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

k = min(n-1, k)

inf = 100000000

dp = [inf for _ in [0]*n]

def cost(i, j):
	return abs(h[i] - h[j])

for i in range(k+1):
	dp[i] = cost(0, i)

for i in range(k+1, n):
	
	dp[i] = min([dp[i-j] + cost(i-j, i) for j in range(1, k+1)])

print(dp[-1])

Buggy python submission (verdict: Runtime Error):
from functools import lru_cache

n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

k = min(n-1, k)

inf = 100000000

dp = [inf for _ in [0]*n]

@lru_cache
def cost(i, j):
	return abs(h[i] - h[j])

for i in range(k+1):
	dp[i] = cost(0, i)

for i in range(k+1, n):
	
	dp[i] = min([dp[i-j] + cost(i-j, i) for j in range(1, k+1)])

print(dp[-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

k = min(n-1, k)

inf = 100000000

dp = [inf for _ in [0]*n]

def cost(i, j):
	return abs(h[i] - h[j])

for i in range(k+1):
	dp[i] = cost(0, i)

for i in range(k+1, n):
	
	dp[i] = min([dp[i-j] + cost(i-j, i) for j in range(1, k+1)])

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

k = min(n-1, k)

inf = 100000000

dp = [inf for _ in [0]*n]

def cost(i, j):
	return abs(h[i] - h[j])

for i in range(k+1):
	dp[i] = cost(0, i)

for i in range(k+1, n):
	
	dp[i] = min([dp[i-j] + cost(i-j, i) for j in range(1, k+1)])

print(dp[-1])


Fixed submission:
n, k = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]

k = min(n-1, k)

inf = 100000000

dp = [inf for _ in [0]*n]

def cost(i, j):
	return abs(h[i] - h[j])

for i in range(k+1):
	dp[i] = cost(0, i)

for i in range(k+1, n):
	
	dp[i] = min([dp[i-j] + cost(i-j, i) for j in range(1, k+1)])

print(dp[-1])
Buggy python submission (verdict: Runtime Error):
from functools import lru_cache

inf = 1000000000

n = int(input())
h = list(map(int, input().split()))

@lru_cache(maxsize=1000)
def dp(i):

    if i == 0:
        return 0
    elif i == 1:
        return abs(h[1] - h[0])
    elif 2 <= i:
        return min(dp(i-1) + abs(h[i]-h[i-1]), dp(i-2) + abs(h[i] - h[i-2]))
    else:
        return inf

print(dp(n-1))


Fixed submission:
n = int(input())
h = list(map(int, input().split()))

dp = [100000] * n

dp[0] = 0

for i in range(0, n):
    if i == 0:
        dp[i] = 0
    elif i == 1:
        dp[i] = abs(h[i-1] - h[i])
    else:
        a = dp[i-1] + abs(h[i] - h[i-1])
        b = dp[i-2] + abs(h[i] - h[i-2])
        dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]))

print(dp[-1])

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)

n = int(input())
h = list(map(int, input().split()))

def dp(i):

    if i == 0:
        return 0
    elif i == 1:
        return abs(h[1] - h[0])
    elif 2 <= i:
        return min(dp(i-1) + abs(h[i]-h[i-1]), dp(i-2) + abs(h[i] - h[i-2]))
    else:
        return 100000000

print(dp(n-1))


Fixed submission:
n = int(input())
h = list(map(int, input().split()))

dp = [100000] * n

dp[0] = 0

for i in range(0, n):
    if i == 0:
        dp[i] = 0
    elif i == 1:
        dp[i] = abs(h[i-1] - h[i])
    else:
        a = dp[i-1] + abs(h[i] - h[i-1])
        b = dp[i-2] + abs(h[i] - h[i-2])
        dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]))

print(dp[-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

def dp(i):

    if i == 0:
        return 0
    elif i == 1:
        return abs(h[1] - h[0])
    elif 2 <= i:
        return min(dp(i-1) + abs(h[i]-h[i-1]), dp(i-2) + abs(h[i] - h[i-2]))
    else:
        return 100000000

print(dp(n-1))


Fixed submission:
n = int(input())
h = list(map(int, input().split()))

dp = [100000] * n

dp[0] = 0

for i in range(0, n):
    if i == 0:
        dp[i] = 0
    elif i == 1:
        dp[i] = abs(h[i-1] - h[i])
    else:
        a = dp[i-1] + abs(h[i] - h[i-1])
        b = dp[i-2] + abs(h[i] - h[i-2])
        dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]))

print(dp[-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

def dp(i):

    if i == 0:
        return 0
    elif i == 1:
        return abs(h[1] - h[0])
    elif 2 <= i:
        return min(dp(i-1) + abs(h[i]-h[i-1]), dp(i-2) + abs(h[i] - h[i-2]))
    else:
        return 100000000

print(dp(n-1))


Fixed submission:
n = int(input())
h = list(map(int, input().split()))

dp = [100000] * n

dp[0] = 0

for i in range(0, n):
    if i == 0:
        dp[i] = 0
    elif i == 1:
        dp[i] = abs(h[i-1] - h[i])
    else:
        a = dp[i-1] + abs(h[i] - h[i-1])
        b = dp[i-2] + abs(h[i] - h[i-2])
        dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]))

print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [1000000000] * n

dp[0] = 0

for i in range(1, n):

    if i == 1:
        dp[1] = abs(h[1] - h[0])
    else:
        dp[i] = min([dp[i-j] + abs(h[i-j] - h[i]) for j in range(1, k+1)])

print(dp[-1])


Fixed submission:
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [1000000000] * n

dp[0] = 0

for i in range(1, n):

    if i == 1:
        dp[1] = abs(h[1] - h[0])
    else:
        dp[i] = min([dp[i-j] + abs(h[i-j] - h[i]) for j in range(1, k+1)])

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split(" "))
arr = list(map(int,input().split(" ")))

l=list()
l.append(0)

for i in range(1,n):
	mini= abs(arr[i] - arr[i-1]) + l[i-1]

	for j in range(2,min(k+1,i+1)):
		mini = min(mini, abs(arr[i] - arr[i-j]) + l[i-j])
	l.append(mini)

# print(l)
print(l[-1])

Fixed submission:
n,k = map(int,input().split(" "))
arr = list(map(int,input().split(" ")))

l=list()
l.append(0)

for i in range(1,n):
	mini= abs(arr[i] - arr[i-1]) + l[i-1]

	for j in range(2,min(k+1,i+1)):
		mini = min(mini, abs(arr[i] - arr[i-j]) + l[i-j])
	l.append(mini)

# print(l)
print(l[-1])
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
 
dp = [0] * n
dp[0] = 0
dp[1] = abs(a[0] - a[1])
#dp[2] = dp[0]
for i in range(2, n):
  dp[i] = min(dp[i-2] + abs(a[i-2] - a[i]) , d[i - 1] + abs(a[i - 1] - a[i]))

print(dp[n - 1])  

Fixed submission:
n = int(input())
a = list(map(int, input().split()))

dp = [0] * n
dp[0] = 0
dp[1] = abs(a[0] - a[1])

for i in range(2, n):
  dp[i] = min(dp[i-2] + abs(a[i-2] - a[i]) , dp[i - 1] + abs(a[i - 1] - a[i]))

print(dp[n - 1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
 
dp = [0] * n
dp[0] = 0
dp[1] = abs(a[0] - a[1])
dp[2] = dp[0]
for i in range(2, n):
  dp[i] = min(dp[i-2] + abs(a[i-2] - a[i]) , d[i - 1] + abs(a[i - 1] - a[i]))
 
print dp[n - 1]  

Fixed submission:
n = int(input())
a = list(map(int, input().split()))

dp = [0] * n
dp[0] = 0
dp[1] = abs(a[0] - a[1])

for i in range(2, n):
  dp[i] = min(dp[i-2] + abs(a[i-2] - a[i]) , dp[i - 1] + abs(a[i - 1] - a[i]))

print(dp[n - 1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))

dp = [0] * n
dp[0] = 0
dp[1] = abs(a[0] - a[1])
dp[2] = dp[0]
for i in range(2, n):
  dp[i] = min(dp[i-2] + abs(a[i-2] - a[i]) , d[i - 1] + abs(a[i - 1] - a[i]))

print(dp[n - 1])  

Fixed submission:
n = int(input())
a = list(map(int, input().split()))

dp = [0] * n
dp[0] = 0
dp[1] = abs(a[0] - a[1])

for i in range(2, n):
  dp[i] = min(dp[i-2] + abs(a[i-2] - a[i]) , dp[i - 1] + abs(a[i - 1] - a[i]))

print(dp[n - 1])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(float, input().split()))

dp = [[0]*(n+1) for i in range(n+1)]
dp[0][0] = 1

for i in range(1,n+1):
  dp[0][i] = 0
  dp[i][0] = dp[i-1][0] * (1 - a[i-1])
  
for i in range(1, n+1):
  for j in range(1, n+1):
    dp[i][j] = dp[i-1][j-1] * a[i-1] + dp[i-1][j] * (1 - a[i-1])

ans = 0
for i in range(n // 2 + 1 , n+1):
  ans += dp[n][i]

print(ans)
  

Fixed submission:
n = int(raw_input())
a = list(map(float, raw_input().split()))
 
dp = [[0]*(n+1) for i in xrange(n+1)]
dp[0][0] = 1
 
for i in xrange(1,n+1):
  dp[0][i] = 0
  dp[i][0] = dp[i-1][0] * (1 - a[i-1])
  
for i in xrange(1, n+1):
  for j in xrange(1, n+1):
    dp[i][j] = dp[i-1][j-1] * a[i-1] + dp[i-1][j] * (1 - a[i-1])
ans = 0
for i in xrange(n // 2 + 1 , n+1):
  ans += dp[n][i]

print ans
Buggy python submission (verdict: Runtime Error):
n = int(input())
dp = [[0]*n for i in range(n)]
for i in range(n):
    l = list(map(int , input().split()))
    if i == 0:
        dp[0][0]=l[0]
        dp[0][0]=l[1]
        dp[0][0]=l[2]
    else:
        dp[i][0]=max(dp[i-1][1],dp[i-1][2])+l[0]
        dp[i][1]=max(dp[i-1][0],dp[i-1][2])+l[1]
        dp[i][2]=max(dp[i-1][0],dp[i-1][1])+l[2]
print(max(dp[-1][0], dp[-1][1], dp[-1][2]))

Fixed submission:
n = int(input())
a = []
b = []
c = []
for i in range(n):
    m,v,p = map(int,input().split())
    a.append(m)
    b.append(v)
    c.append(p)
d = [[0]*3 for i in range(n)]
d[0][0]=a[0]
d[0][1]=b[0]
d[0][2]=c[0]
for i in range(1,n):
    for j in range(3):
        if j == 0:
            d[i][j] = max(d[i-1][1],d[i-1][2]) + a[i]
        elif j == 1:
            d[i][j] = max(d[i-1][0],d[i-1][2]) + b[i]
        else:
            d[i][j] = max(d[i-1][0],d[i-1][1]) + c[i]
print(max(d[-1][0],d[-1][1],d[-1][2]))
Buggy python submission (verdict: Runtime Error):
from typing import List
import sys

n = int(input())
h_list: List[int] = [int(x) for x in input().split(" ")]

dp: List[int] = [sys.maxsize] * len(h_list)
dp[0] = 0
dp[1] = abs(h_list[1] - h_list[0])
for i in range(2, len(dp)):
  dp[i] = min(dp[i - 1] + abs(h_list[i] - h_list[i-1]), dp[i - 2] + abs(h_list[i] - h_list[i - 2]))

print(dp[-1])

Fixed submission:
import sys

n = int(input())
h_list = [int(x) for x in input().split(" ")]

dp = [sys.maxsize] * len(h_list)
dp[0] = 0
dp[1] = abs(h_list[1] - h_list[0])
for i in range(2, len(dp)):
  dp[i] = min(dp[i - 1] + abs(h_list[i] - h_list[i-1]), dp[i - 2] + abs(h_list[i] - h_list[i - 2]))

print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys

n, k = [int(x) for x in input().split(" ")]
h_list = [int(x) for x in input().split(" ")]

dp = [sys.maxsize] * len(h_list)
dp[0] = 0
dp[1] = abs(h_list[1] - h_list[0])

for i in range(2, len(dp)):
  dp_i_costs = []
  for j in range(k+1):
    if i - j >= 0:
      dp_i_costs.append(dp[i - j] + abs(h_list[i] - h_list[i - j]))
  
  dp[i] = min(dp_i_costs)

print(dp[-1])

Fixed submission:
import sys

n, k = [int(x) for x in input().split(" ")]
h_list = [int(x) for x in input().split(" ")]

dp = [sys.maxsize] * len(h_list)
dp[0] = 0
dp[1] = abs(h_list[1] - h_list[0])

for i in range(2, len(dp)):
  dp_i_costs = []
  for j in range(k+1):
    if i - j >= 0:
      dp_i_costs.append(dp[i - j] + abs(h_list[i] - h_list[i - j]))
  
  dp[i] = min(dp_i_costs)

print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
import fileinput


def frog(stones, jump_range):
    stone_cost = [0]
    for i in range(1, len(stones)):
        stone_cost.append(min(
            stone_cost[i - curr_jmp] + abs(stones[i] - stones[i - curr_jmp])
            for curr_jmp in range(1, min(i, jump_range) + 1)
        ))
    return stone_cost[-1]


inp = fileinput.input()
jump_range = [int(v) for v in inp.readline().split(" ")][1]
stones = [int(v) for v in inp.readline().split(" ")]
print(frog(stones, jump_range))

Fixed submission:
import fileinput


def frog(stones, jump_range):
    stone_cost = [0]
    for i in range(1, len(stones)):
        stone_cost.append(min(
            stone_cost[i - curr_jmp] + abs(stones[i] - stones[i - curr_jmp])
            for curr_jmp in range(1, min(i, jump_range) + 1)
        ))
    return stone_cost[-1]


inp = fileinput.input()
jump_range = [int(v) for v in inp.readline().split(" ")][1]
stones = [int(v) for v in inp.readline().split(" ")]
print(frog(stones, jump_range))