Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    #初期値は0
    def Num(dest,flog,h):
        q=dest-1
        if flog[q]>0:
            return flog[q]
        if dest<k+2:
            res=abs(h[q]-h[0])
            flog[q]=res
            return res
        
        min=Num(q,flog,h)+abs(h[q]-h[q-1])
        for i in range(2,k+1):
            tmp=Num(dest-i,flog,h)+abs(h[q]-h[q-i])
            if min>tmp:
                min=tmp
        flog[q]=min
        return min   
    print(Num(n,flog,h))
if __name__=="__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    #初期値は0
    def Num(dest,flog,h):
        q=dest-1
        if flog[q]>0:
            return flog[q]
        if dest<k+2:
            res=abs(h[q]-h[0])
            flog[q]=res
            return res
        
        min=Num(q)+abs(h[q]-h[q-1])
        for i in range(2,k+1):
            tmp=Num(dest-i,flog,h)+abs(h[q]-h[q-i])
            if min>tmp:
                min=tmp
        flog[q]=min
        return min   
    print(Num(n,flog,h))
if __name__=="__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    #初期値は0
    def Num(dest,flog,h):
        q=dest-1
        if flog[q]>0:
            return flog[q]
        if dest<k+2:
            res=abs(h[q]-h[0])
            flog[q]=res
            return res
        
        min=Num(q)+abs(h[q]-h[q-1])
        for i in range(2,k+1):
            tmp=Num(dest-i)+abs(h[q]-h[q-i])
            if min>tmp:
                min=tmp
        flog[q]=min
        return min   
    print(Num(n,flog,h))
if __name__=="__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    #初期値は0
    def Num(dest):
        q=dest-1
        if flog[q]>0:
            return flog[q]
        if dest<k+2:
            res=abs(h[q]-h[0])
            flog[q]=res
            return res
        
        min=Num(q)+abs(h[q]-h[q-1])
        for i in range(2,k+1):
            tmp=Num(dest-i)+abs(h[q]-h[q-i])
            if min>tmp:
                min=tmp
        flog[q]=min
        return min   
    print(Num(n))
if __name__=="__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    q=dest-1
    if flog[q]>0:
        return flog[q]
    if dest<k+2:
        res=abs(h[q]-h[0])
        flog[q]=res
        return res
    
    min=Num(q)+abs(h[q]-h[q-1])
    for i in range(2,k+1):
        tmp=Num(dest-i)+abs(h[q]-h[q-i])
        if min>tmp:
            min=tmp
    flog[q]=min
    return min   
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    if flog[dest-1]>0:
        return flog[dest-1]
    if dest<k+2:
        res=abs(h[dest-1]-h[0])
        flog[dest-1]=res
        return res
    else:
        min=Num(dest-1)+abs(h[dest-1]-h[dest-1-1])
        for i in range(2,k+1):
            tmp=Num(dest-i)+abs(h[dest-1]-h[dest-i-1])
            if min>tmp:
                min=tmp
        flog[dest-1]=min
        return min   
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    if flog[dest-1]!=-1:
        return flog[dest-1]
    if dest<k+2:
        res=abs(h[dest-1]-h[0])
        flog[dest-1]=res
        return res
    else:
        min=float("inf")
        for i in range(1,k+1):
            tmp=Num(dest-i)+abs(h[dest-1]-h[dest-i-1])
            if min>tmp:
                min=tmp
        flog[dest-1]=min
        return min   
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    if flog[dest-1]!=-1:
        return flog[dest-1]
    if dest<k+2:
        res=abs(h[dest-1]-h[0])
        flog[dest-1]=res
        return res
    else:
        min=10000000000
        for i in range(1,k+1):
            tmp=Num(dest-i)+abs(h[dest-1]-h[dest-i-1])
            if min>tmp:
                min=tmp
        flog[dest-1]=min
        return min   
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    if flog[dest-1]!=-1:
        return flog[dest-1]
    if dest<k+2:
        res=abs(h[dest-1]-h[0])
        flog[dest-1]=res
        return res
    else:
        min=10**10
        for i in range(1,k+1):
            tmp=Num(dest-i)+abs(h[dest-1]-h[dest-i-1])
            if min>tmp:
                min=tmp
        flog[dest-1]=min
        return min   
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    if flog[dest-1]!=-1:
        return flog[dest-1]
    if dest<k+2:
        res=abs(h[dest-1]-h[0])
        flog[dest-1]=res
        return res
    else:
        min=10**10
        for i in range(1,k+1):
            tmp=Num(dest-i)+abs(h[dest-1]-h[dest-i-1])
            if min>tmp:
                min=tmp
        flog[dest-1]=min
        return min
          

    
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    if flog[dest-1]!=-1:
        return flog[dest-1]
    if dest<k+2:
        res=abs(h[dest-1]-h[0])
        flog[dest-1]=res
        return res
    else:
        min=10**4*n
        for i in range(1,k+1):
            tmp=Num(dest-i)+abs(h[dest-1]-h[dest-i-1])
            if min>tmp:
                min=tmp
        flog[dest-1]=min
        return min
          

    
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(400000000)
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    if flog[dest-1]!=-1:
        return flog[dest-1]
    if dest<k+2:
        res=abs(h[dest-1]-h[0])
        flog[dest-1]=res
        return res
    else:
        min=10**4*n
        for i in range(1,k+1):
            tmp=Num(dest-i)+abs(h[dest-1]-h[dest-i-1])
            if min>tmp:
                min=tmp
        flog[dest-1]=min
        return min
          

    
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
h=list(map(int,input().split()))
flog=[-1]*n
#初期値は0
def Num(dest):
    if flog[dest-1]!=-1:
        return flog[dest-1]
    if dest<k+2:
        res=abs(h[dest-1]-h[0])
        flog[dest-1]=res
        return res
    else:
        min=10**4*n
        for i in range(1,k+1):
            tmp=Num(dest-i)+abs(h[dest-1]-h[dest-i-1])
            if min>tmp:
                min=tmp
        flog[dest-1]=min
        return min
          

    
print(Num(n))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
#まずｎ団目までいく最短コストを探す。それはｎからｋ前までからのうち最短コストとなるもの
import sys #追加
sys.setrecursionlimit(200000000000000)
def minJ(N,K,costL,memo):
    if N-1 in memo ==True:
        return memo[N-1]
    if N<K:
        return abs(costL[N-1]-costL[0])
    else:
        result=None
        for i in range(1,K+1):
            stock=minJ(N-i,K,costL,memo)+abs(costL[N-1]-costL[N-i-1])
            if result==None or (stock<result):
                result=stock
    memo[N-1]=result
    return result

memo={}
N,K=map(int, input().split())
costL=list(map(int,input().split()))
print(minJ(N,K,costL,memo))


Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
#まずｎ団目までいく最短コストを探す。それはｎからｋ前までからのうち最短コストとなるもの
import sys #追加
sys.setrecursionlimit(2147483647)
def minJ(N,K,costL,memo):
    if N-1 in memo ==True:
        return memo[N-1]
    if N<K:
        return abs(costL[N-1]-costL[0])
    else:
        result=None
        for i in range(1,K+1):
            stock=minJ(N-i,K,costL,memo)+abs(costL[N-1]-costL[N-i-1])
            if result==None or (stock<result):
                result=stock
    memo[N-1]=result
    return result

memo={}
a,b=map(int, input().split())
costL=[]
try:
    costL.append(int(input()))
except:
    EOFError
print(minJ(a,b,costL,memo))


Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
#まずｎ団目までいく最短コストを探す。それはｎからｋ前までからのうち最短コストとなるもの
import sys #追加
sys.setrecursionlimit(2147483647)
def minJ(N,K,costL,memo):
    if N-1 in memo ==True:
        return memo[N-1]
    if N<K:
        return abs(costL[N-1]-costL[0])
    else:
        result=None
        for i in range(1,K+1):
            stock=minJ(N-i,K,costL,memo)+abs(costL[N-1]-costL[N-i-1])
            if result==None or (stock<result):
                result=stock
    memo[N-1]=result
    return result
N=int(input())
K=int(input())
costL=[]
memo={}
costL=list(map(int, input().split())) 
print(minJ(N,K,costL,memo))


Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
#まずｎ団目までいく最短コストを探す。それはｎからｋ前までからのうち最短コストとなるもの
import sys #追加
sys.setrecursionlimit(2147483647)
def minJ(N,K,costL,memo):
    if N-1 in memo ==True:
        return memo[N-1]
    if N<K:
        return abs(costL[N-1]-costL[0])
    else:
        result=None
        for i in range(1,K+1):
            stock=minJ(N-i,K,costL,memo)+abs(costL[N-1]-costL[N-i-1])
            if result==None or (stock<result):
                result=stock
    memo[N-1]=result
    return result
N=int(input())
K=int(input())
costL=[]
memo={}

for i in range(N):
    costL.append(int(input()))

print(minJ(N,K,costL,memo))


Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
#まずｎ団目までいく最短コストを探す。それはｎからｋ前までからのうち最短コストとなるもの
import sys #追加
sys.setrecursionlimit(500*500) #追加
def minJ(N,K,costL,memo):
    if N-1 in memo ==True:
        return memo[N-1]
    if N<K:
        return abs(costL[N-1]-costL[0])
    else:
        result=None
        for i in range(1,K+1):
            stock=minJ(N-i,K,costL,memo)+abs(costL[N-1]-costL[N-i-1])
            if result==None or (stock<result):
                result=stock
    memo[N-1]=result
    return result
N=int(input())
K=int(input())
costL=[]
memo={}

for i in range(N):
    costL.append(int(input()))

print(minJ(N,K,costL,memo))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def minJ(N,K,costL,memo):
    if N-1 in memo ==True:
        return memo[N-1]
    if N<K:
        return abs(costL[N-1]-costL[0])
    else:
        result=None
        for i in range(1,K+1):
            stock=minJ(N-i,K,costL,memo)+abs(costL[N-1]-costL[N-i-1])
            if result==None or (stock<result):
                result=stock
    memo[N-1]=result
    return result
N=int(input())
K=int(input())
costL=[]
memo={}

for i in range(N):
    costL.append(int(input()))

print(minJ(N,K,costL,memo))

Fixed submission:
import sys
sys.setrecursionlimit(400000000)
def main():
    n,k=map(int,input().split())
    h=tuple(map(int,input().split()))
    flog=[-1]*n
    for i in range(n):#dest=i 0が１
        if i<k+1:#0~k
            flog[i]=abs(h[i]-h[0])
        else:
            min=10**9
            for j in range(1,k+1):#1~k
                tmp=flog[i-j]+abs(h[i]-h[i-j])
                if tmp<min:
                    min=tmp
            flog[i]=min
    print(flog[n-1])
   # print(flog)
if __name__=="__main__":
    main()