Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    import numpy as np

    N, K = map(int, input().split())
    H = np.array(input().split(), 'int32')

    INF = 10 ** 10
    dp = np.full(N, INF, 'int64')
    dp[0] = 0
    for i in range(N):
        target = dp[i]
        now_height = H[i]
        for k in range(1, K + 1 if i + K < N else N - i):
            res = target + abs(now_height - H[i + k])
            cal = dp[i + k]
            dp[i + k] = res if res < cal else cal
        # right = min(i + K, N)
        # dp[i + 1:right] = np.min(dp[i] + np.abs(H[i] - H[i + 1:right]))
    print(dp[N - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def resolve():
    import sys
    
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = [int(x) for x in readline().split()]

    inf = float('inf')
    dp = [inf] * N
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        for j in range(leng):
            cal = dp[i - j] + abs(h[i] - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[-1])
            

resolve()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    import numpy as np

    N, K = map(int, input().split())
    # H = [int(x) for x in input().split()] + [0] * K
    H = np.array(input().split(), 'int32')

    INF = 10 ** 10
    # dp = [INF] * (N + K)
    dp = np.full(N, INF, 'int64')
    dp[0] = 0
    # for i in range(N):
    #     target = dp[i]
    #     for k in range(1, K + 1):
    #         res = target + abs(H[i] - H[i + k])
    #         dp[i + k] = res if res < dp[i + k] else dp[i + k]
    for i in range(N):
        target = dp[i]
        for k in range(1, K + 1):
            if i + k >= N:
                break
            res = target + abs(H[i] - H[i + k])
            dp[i + k] = res if res < dp[i + k] else dp[i + k]
        # right = min(i + K, N)
        # dp[i:right] = np.min(dp[i:right], dp[i] + np.abs(H[i] - H[i:right]))
    print(dp[N - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def resolve():
    import sys
    
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = [int(x) for x in readline().split()]

    inf = float('inf')
    dp = [inf] * N
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        for j in range(leng):
            cal = dp[i - j] + abs(h[i] - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[-1])
            

resolve()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    H = [int(x) for x in input().split()] + [0] * K

    INF = 10 ** 10
    dp = [INF] * (N + K)
    dp[0] = 0
    for i in range(N):
        target = dp[i]
        for k in range(1, K + 1):
            res = target + abs(H[i] - H[i + k])
            dp[i + k] = res if res < dp[i + k] else dp[i + k]
    print(dp[N - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def resolve():
    import sys
    
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = [int(x) for x in readline().split()]

    inf = float('inf')
    dp = [inf] * N
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        for j in range(leng):
            cal = dp[i - j] + abs(h[i] - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[-1])
            

resolve()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    H = [int(x) for x in input().split()] + [0] * 200

    INF = 10 ** 10
    dp = [INF] * 10 ** 6
    dp[0] = 0
    for i in range(N):
        for k in range(1, K + 1):
            dp[i + k] = min(dp[i + k], dp[i] + abs(H[i] - H[i + k]))
    print(dp[N - 1])


if __name__ == '__main__':
    main()


Fixed submission:
def resolve():
    import sys
    
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = [int(x) for x in readline().split()]

    inf = float('inf')
    dp = [inf] * N
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        for j in range(leng):
            cal = dp[i - j] + abs(h[i] - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[-1])
            

resolve()

Buggy python submission (verdict: Time Limit Exceeded):
def resolve():
    import sys
    import numpy as np

    read = sys.stdin.read
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = np.array(read().split(), np.int32)

    # 貰うDP
    inf = 10 ** 9
    dp = np.full(N, inf, np.int32)
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        val = h[i]
        for j in range(leng):
            # 高速化ののため，min関数ではなくif文を使用
            cal = dp[i - j] + abs(val - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[-1])
            

resolve()


Fixed submission:
def resolve():
    import sys
    
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = [int(x) for x in readline().split()]

    inf = float('inf')
    dp = [inf] * N
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        for j in range(leng):
            cal = dp[i - j] + abs(h[i] - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[-1])
            

resolve()

Buggy python submission (verdict: Time Limit Exceeded):
def resolve():
    import sys
    import numpy as np

    read = sys.stdin.read
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = np.array(read().split(), np.int32)

    # 貰うDP
    inf = 10 ** 9
    dp = np.full(N + K, inf, np.int32)
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        for j in range(leng):
            # 高速化ののため，min関数ではなくif文を使用
            cal = dp[i - j] + abs(h[i] - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[N - 1])
            

resolve()


Fixed submission:
def resolve():
    import sys
    
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = [int(x) for x in readline().split()]

    inf = float('inf')
    dp = [inf] * N
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        for j in range(leng):
            cal = dp[i - j] + abs(h[i] - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[-1])
            

resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys


def resolve():
    readline = sys.stdin.readline    # 1行だけ文字列にする

    N, K = map(int, readline().split())
    h = [int(x) for x in readline().split()]

    inf = float('inf')
    dp = [inf] * N
    dp[0] = 0

    for i in range(1, N):
        if i < K:
            for j in range(i + 1):
                dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
        else:
            for j in range(K + 1):
                dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
    print(dp[N - 1])
            

resolve()


Fixed submission:
def resolve():
    import sys
    
    readline = sys.stdin.readline

    N, K = map(int, readline().split())
    h = [int(x) for x in readline().split()]

    inf = float('inf')
    dp = [inf] * N
    dp[0] = 0

    for i in range(1, N):
        leng = i + 1 if i < K else K + 1
        for j in range(leng):
            cal = dp[i - j] + abs(h[i] - h[i - j])
            if dp[i] > cal:
                dp[i] = cal

    print(dp[-1])
            

resolve()