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

input = sys.stdin.readline


def main():
    # 2 <= N <= pow(10,5), 1 <= K <= 100
    N, K = [int(ele) for ele in input().split()]
    h = [Node(int(ele)) for ele in input().split()]

    for idx, n in enumerate(h):
        s = max(0, idx - K)
        n.parents = h[s:idx]
        n.cal_dp()

    print(h[-1].dp_val)


class Node():

    def __init__(self, val):
        self.val = val
        self.dp_val = 1000000000000
        self.parents = []

    def cal_dp(self):
        if self.parents == []:
            self.dp_val = 0
            return

        for n in self.parents:
            self.dp_val = min(self.dp_val, abs(n.val - self.val)+n.dp_val)


if __name__ == '__main__':
    main()


Fixed submission:
# python3
import sys

input = sys.stdin.readline


def main():
    # 2 <= N <= pow(10,5), 1 <= K <= 100
    N, K = [int(ele) for ele in input().split()]
    h = [Node(int(ele)) for ele in input().split()]

    for idx, n in enumerate(h):
        s = max(0, idx - K)
        n.parents = h[s:idx]
        n.cal_dp()

    print(h[-1].dp_val)


class Node():

    def __init__(self, val):
        self.val = val
        self.dp_val = 1000000000000
        self.parents = []

    def cal_dp(self):
        if self.parents == []:
            self.dp_val = 0
            return

        for n in self.parents:
            self.dp_val = min(self.dp_val, abs(n.val - self.val)+n.dp_val)


if __name__ == '__main__':
    main()

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

input = sys.stdin.readline


def main():
    # 2 <= N <= pow(10,5), 1 <= K <= 100
    N, K = [int(ele) for ele in input().split()]
    h = [Node(int(ele)) for ele in input().split()]

    for idx, n in enumerate(h):
        if idx != 0:
            s = max(0, idx - K)
            n.parents = h[s:idx]

    for n in h:
        n.cal_dp()

    print(h[-1].dp_val)


class Node():

    def __init__(self, val):
        self.val = val
        self.dp_val = 1000000000000
        self.parents = []

    def cal_dp(self):
        if self.parents == []:
            self.dp_val = 0
            return

        for n in self.parents:
            self.dp_val = min(self.dp_val, abs(n.val - self.val)+n.dp_val)


if __name__ == '__main__':
    main()


Fixed submission:
# python3
import sys

input = sys.stdin.readline


def main():
    # 2 <= N <= pow(10,5), 1 <= K <= 100
    N, K = [int(ele) for ele in input().split()]
    h = [Node(int(ele)) for ele in input().split()]

    for idx, n in enumerate(h):
        s = max(0, idx - K)
        n.parents = h[s:idx]
        n.cal_dp()

    print(h[-1].dp_val)


class Node():

    def __init__(self, val):
        self.val = val
        self.dp_val = 1000000000000
        self.parents = []

    def cal_dp(self):
        if self.parents == []:
            self.dp_val = 0
            return

        for n in self.parents:
            self.dp_val = min(self.dp_val, abs(n.val - self.val)+n.dp_val)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# python3


def main():
    # 2 <= N <= pow(10,5), 1 <= K <= 100
    N, K = [int(ele) for ele in input().split()]
    h = [Node(int(ele)) for ele in input().split()]

    for idx, n in enumerate(h):
        if idx != 0:
            s = max(0, idx - K)
            n.parents = h[s:idx]

    for n in h:
        n.cal_dp()

    print(h[-1].dp_val)


class Node():

    def __init__(self, val):
        self.val = val
        self.dp_val = 1000000000000
        self.parents = []

    def cal_dp(self):
        if self.parents == []:
            self.dp_val = 0
            return

        for n in self.parents:
            self.dp_val = min(self.dp_val, abs(n.val - self.val)+n.dp_val)


if __name__ == '__main__':
    main()


Fixed submission:
# python3
import sys

input = sys.stdin.readline


def main():
    # 2 <= N <= pow(10,5), 1 <= K <= 100
    N, K = [int(ele) for ele in input().split()]
    h = [Node(int(ele)) for ele in input().split()]

    for idx, n in enumerate(h):
        s = max(0, idx - K)
        n.parents = h[s:idx]
        n.cal_dp()

    print(h[-1].dp_val)


class Node():

    def __init__(self, val):
        self.val = val
        self.dp_val = 1000000000000
        self.parents = []

    def cal_dp(self):
        if self.parents == []:
            self.dp_val = 0
            return

        for n in self.parents:
            self.dp_val = min(self.dp_val, abs(n.val - self.val)+n.dp_val)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# python3


def main():
    # 2 <= N <= pow(10,5), 1 <= K <= 100
    N, K = [int(ele) for ele in input().split()]
    h = [Node(int(ele)) for ele in input().split()]
    cal_dp(h, K)
    print(h[-1].dp_val)


def cal_dp(h, K):
    for idx, n in enumerate(h):
        if idx == 0:
            n.dp_val = 0
        else:
            tmp = [pow(10, 5)] * (K)
            for jdx in range(1, K+1):
                if jdx <= idx:
                    f = h[idx-jdx].dp_val
                    diff = abs(h[idx].val - h[idx-jdx].val)
                    tmp[jdx-1] = f + diff
                else:
                    break

            n.dp_val = min(tmp)


class Node():

    def __init__(self, val):
        self.val = val
        self.dp_val = 'unk'


if __name__ == '__main__':
    main()


Fixed submission:
# python3
import sys

input = sys.stdin.readline


def main():
    # 2 <= N <= pow(10,5), 1 <= K <= 100
    N, K = [int(ele) for ele in input().split()]
    h = [Node(int(ele)) for ele in input().split()]

    for idx, n in enumerate(h):
        s = max(0, idx - K)
        n.parents = h[s:idx]
        n.cal_dp()

    print(h[-1].dp_val)


class Node():

    def __init__(self, val):
        self.val = val
        self.dp_val = 1000000000000
        self.parents = []

    def cal_dp(self):
        if self.parents == []:
            self.dp_val = 0
            return

        for n in self.parents:
            self.dp_val = min(self.dp_val, abs(n.val - self.val)+n.dp_val)


if __name__ == '__main__':
    main()