Buggy python submission (verdict: Runtime Error):
#!/mnt/c/Users/moiki/bash/env/bin/python
# FROM : https://atcoder.jp/contests/dp/submissions/4017206
# N,M = map(int, input().split())
N = int(input())
from collections import Counter
a = Counter( map(int, input().split()))


#1,2,3
A,B,C = (a[i] for i in range(1,4))

# dpA,B,C

dp = [ [ [0] * (C+1) 
           for _ in range(B+C+1)
       ]
           for _ in range(A+B+C+1) 
     ]


for c in range(C+1):
    for b in range(B+C+1-c):
        for a in range(A+B+C+1-b-c):

            try: 
                dp[a][b][c] = (
                        ( a * ( dp[a-1][b][c]   if a else 0)  ) + 
                        ( b * ( dp[a+1][b-1][c] if b else 0)  ) +
                        ( c * ( dp[a][b+1][c-1] if c else 0)  ) + N 
                       ) / ( a+b+c ) 
            except:
                continue

print(dp[A][B][C])


Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))

# (N,K), h = map(int, input().split()), list(map(int, input().split()))
N,K,*h = map(int, "\n".join([input(), input()]).split())

# suml = [1e9 for i in range(N)]
# suml[0] = 0


suml = [0]*N

# for i in range(1,N):
for i,e_h in enumerate(h):
    if i == 0:
        continue

    start = max(0, i-K)
    cost  = min(cst + abs( e_h - hgh) for cst, hgh in 
                   zip(suml[start:i], h[start:i]))
    # cost  = min(cst + abs( h[i] - hgh ) for cst, hgh in 
    #               zip(suml[start:i], h[start:i]))
    suml[i] = cost

print(suml[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))

# (N,K), h = map(int, input().split()), list(map(int, input().split()))
N,K,*h = map(int, "\n".join([input(), input()]).split())

# suml = [1e9 for i in range(N)]
# suml[0] = 0


suml = [0]*N

for i in range(1,N):

    start = max(0, i-K)
    cost  = min(cst + abs( h[i] - hgh ) for cst, hgh in 
                  zip(suml[start:i], h[start:i]))
    suml[i] = cost

print(suml[-1])
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))

# (N,K), h = map(int, input().split()), list(map(int, input().split()))
N,K,*h = map(int, "\n".join([input(), input()]).split())

# suml = [1e9 for i in range(N)]
# suml[0] = 0


suml = [0]*N

# for i in range(1,N):
for i,e_h in enumerate(h):
    if i == 0:
        continue

    start = max(0, i-K)
    cost  = min(cst + abs( e_h - hgh) for cst, hgh in 
                   zip(suml[start:i], h[start:i]))
    # cost  = min(cst + abs( h[i] - hgh ) for cst, hgh in 
    #               zip(suml[start:i], h[start:i]))
    suml[i] = cost

print(suml[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))
(N,K), h = map(int, input().split()), list(map(int, input().split()))

# suml = [1e9 for i in range(N)]
# suml[0] = 0
suml = [0]*N

for e in range(1,N):

    # for k in range(1, min([e, K])+1):
    #     suml[e] = min(suml[e],
    #             abs(h[e]-h[e-k]) + suml[e-k]
    #     )
    start = max(0, e-K)
    cost  = min(cst + abs( h[e] - hgh ) for cst, hgh in zip(suml[start:e], 
           h[start:e]))
    suml[e] = cost

print(suml[N-1])
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))

# (N,K), h = map(int, input().split()), list(map(int, input().split()))
N,K,*h = map(int, "\n".join([input(), input()]).split())

# suml = [1e9 for i in range(N)]
# suml[0] = 0


suml = [0]*N

# for i in range(1,N):
for i,e_h in enumerate(h):
    if i == 0:
        continue

    start = max(0, i-K)
    cost  = min(cst + abs( e_h - hgh) for cst, hgh in 
                   zip(suml[start:i], h[start:i]))
    # cost  = min(cst + abs( h[i] - hgh ) for cst, hgh in 
    #               zip(suml[start:i], h[start:i]))
    suml[i] = cost

print(suml[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = list(map(int, input().split()))
h = list(map(int, input().split()))

# suml = [1e9 for i in range(N)]
# suml[0] = 0
suml = [0]*N

for e in range(1,N):

    # for k in range(1, min([e, K])+1):
    #     suml[e] = min(suml[e],
    #             abs(h[e]-h[e-k]) + suml[e-k]
    #     )
    start = max(0, e-K)
    cost  = min(cst + abs( h[e] - hgh ) for cst, hgh in zip(suml[start:e], 
           h[start:e]))
    suml[e] = cost

print(suml[N-1])
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))

# (N,K), h = map(int, input().split()), list(map(int, input().split()))
N,K,*h = map(int, "\n".join([input(), input()]).split())

# suml = [1e9 for i in range(N)]
# suml[0] = 0


suml = [0]*N

# for i in range(1,N):
for i,e_h in enumerate(h):
    if i == 0:
        continue

    start = max(0, i-K)
    cost  = min(cst + abs( e_h - hgh) for cst, hgh in 
                   zip(suml[start:i], h[start:i]))
    # cost  = min(cst + abs( h[i] - hgh ) for cst, hgh in 
    #               zip(suml[start:i], h[start:i]))
    suml[i] = cost

print(suml[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = list(map(int, input().split()))
h = list(map(int, input().split()))

suml = [1e9 for i in range(N)]
suml[0] = 0

for e in range(1,N):

    # for k in range(1, min([e, K])+1):
    #     suml[e] = min(suml[e],
    #             abs(h[e]-h[e-k]) + suml[e-k]
    #     )
    start = max(0, e-K)
    cost  = min(cst + abs( h[e] - hgh ) for cst, hgh in zip(suml[start:e], 
           h[start:e]))
    suml[e] = cost

print(suml[N-1])
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))

# (N,K), h = map(int, input().split()), list(map(int, input().split()))
N,K,*h = map(int, "\n".join([input(), input()]).split())

# suml = [1e9 for i in range(N)]
# suml[0] = 0


suml = [0]*N

# for i in range(1,N):
for i,e_h in enumerate(h):
    if i == 0:
        continue

    start = max(0, i-K)
    cost  = min(cst + abs( e_h - hgh) for cst, hgh in 
                   zip(suml[start:i], h[start:i]))
    # cost  = min(cst + abs( h[i] - hgh ) for cst, hgh in 
    #               zip(suml[start:i], h[start:i]))
    suml[i] = cost

print(suml[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = list(map(int, input().split()))
h = list(map(int, input().split()))

suml = [1e9 for i in range(N)]
suml[0] = 0
# print(suml)

for e in range(1,N):
    # print("min: ", min([e, K+1]))
    # print("range: ", 1, min(e,K+1))

    for k in range(1, min([e, K])+1):
        # print("\tk: ", k)
        # print("\te-k:", e-k)
        # print("\te: ", e)
        suml[e] = min(suml[e],
                abs(h[e]-h[e-k]) + suml[e-k]
        )
        # print("suml[e] : ", 
        #         abs(h[e]-h[e-k]) + suml[e-k] ) 

print(suml[N-1])
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))

# (N,K), h = map(int, input().split()), list(map(int, input().split()))
N,K,*h = map(int, "\n".join([input(), input()]).split())

# suml = [1e9 for i in range(N)]
# suml[0] = 0


suml = [0]*N

# for i in range(1,N):
for i,e_h in enumerate(h):
    if i == 0:
        continue

    start = max(0, i-K)
    cost  = min(cst + abs( e_h - hgh) for cst, hgh in 
                   zip(suml[start:i], h[start:i]))
    # cost  = min(cst + abs( h[i] - hgh ) for cst, hgh in 
    #               zip(suml[start:i], h[start:i]))
    suml[i] = cost

print(suml[-1])

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,K = list(map(int, input().split()))
h = list(map(int, input().split()))

suml = [1e9 for i in range(N)]
suml[0] = 0

for e in range(1,N):
    for k in range(1,K+1):
        if not ((e-k) >= 0):
            continue
        suml[e] = min(suml[e],
                abs(h[e]-h[e-k]) + suml[e-k]
        )

print(suml[N-1])
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,K = list(map(int, input().split()))
# h = list(map(int, input().split()))

# (N,K), h = map(int, input().split()), list(map(int, input().split()))
N,K,*h = map(int, "\n".join([input(), input()]).split())

# suml = [1e9 for i in range(N)]
# suml[0] = 0


suml = [0]*N

# for i in range(1,N):
for i,e_h in enumerate(h):
    if i == 0:
        continue

    start = max(0, i-K)
    cost  = min(cst + abs( e_h - hgh) for cst, hgh in 
                   zip(suml[start:i], h[start:i]))
    # cost  = min(cst + abs( h[i] - hgh ) for cst, hgh in 
    #               zip(suml[start:i], h[start:i]))
    suml[i] = cost

print(suml[-1])